Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
cptmorgan-rh authored Oct 23, 2024
2 parents 96d6152 + 45e91af commit 9e356ba
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 95 deletions.
17 changes: 10 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
&& go version
- name: Build
run: |
echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
TAG="${GITHUB_REF#refs/tags/}"
HASH=$(git log -n1 --pretty=format:%h)
echo ${TAG} ${HASH}
Expand All @@ -32,9 +32,9 @@ jobs:
&& env GOOS=linux CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X 'github.com/gmeghnag/omc/vars.OMCVersionTag=${TAG}' -X github.com/gmeghnag/omc/vars.OMCVersionHash=${HASH}" -o omc \
&& tar -cvzf omc_Linux_arm64.tar.gz omc && cp omc_Linux_arm64.tar.gz omc_Linux_aarch64.tar.gz && rm -rf omc \
&& env GOOS=darwin CGO_ENABLED=0 GOARCH=amd64 go build -ldflags "-X 'github.com/gmeghnag/omc/vars.OMCVersionTag=${TAG}' -X github.com/gmeghnag/omc/vars.OMCVersionHash=${HASH}" -o omc \
&& cp omc omc_Darwin_x86_64 && tar -cvzf omc_Darwin_x86_64.tar.gz omc && rm -rf omc \
&& cp omc omc_Darwin_x86_64 && cp omc omc_Darwin_amd64 && tar -cvzf omc_Darwin_x86_64.tar.gz omc && rm -rf omc \
&& env GOOS=darwin CGO_ENABLED=0 GOARCH=arm64 go build -ldflags "-X 'github.com/gmeghnag/omc/vars.OMCVersionTag=${TAG}' -X github.com/gmeghnag/omc/vars.OMCVersionHash=${HASH}" -o omc \
&& tar -cvzf omc_Darwin_arm64.tar.gz omc && cp omc_Darwin_arm64.tar.gz omc_Darwin_aarch64.tar.gz && rm -rf omc \
&& cp omc omc_Darwin_arm64 && tar -cvzf omc_Darwin_arm64.tar.gz omc && cp omc_Darwin_arm64.tar.gz omc_Darwin_aarch64.tar.gz && rm -rf omc \
&& env GOOS=windows CGO_ENABLED=0 GOARCH=amd64 go build -ldflags "-X 'github.com/gmeghnag/omc/vars.OMCVersionTag=${TAG}' -X github.com/gmeghnag/omc/vars.OMCVersionHash=${HASH}" -o omc.exe \
&& zip omc_Windows_x86_64.zip omc.exe \
&& md5sum omc_Linux_x86_64.tar.gz | tee -a checksums.txt \
Expand All @@ -44,6 +44,7 @@ jobs:
&& md5sum omc_Darwin_arm64.tar.gz | tee -a checksums.txt \
&& md5sum omc_Darwin_aarch64.tar.gz | tee -a checksums.txt \
&& md5sum omc_Windows_x86_64.zip | tee -a checksums.txt \
&& md5sum omc_Darwin_arm64 | tee -a checksums.txt \
&& md5sum omc_Darwin_x86_64 | tee -a checksums.txt \
&& md5sum omc_Linux_x86_64| tee -a checksums.txt
- name: Release
Expand All @@ -53,12 +54,14 @@ jobs:
files: |
checksums.txt
omc_Linux_x86_64
omc_Linux_x86_64.tar.gz
omc_Linux_arm64.tar.gz
omc_Linux_x86_64.tar.gz
omc_Linux_arm64.tar.gz
omc_Linux_aarch64.tar.gz
omc_Darwin_x86_64
omc_Darwin_x86_64.tar.gz
omc_Darwin_arm64.tar.gz
omc_Darwin_aarch64.tar.gz
omc_Darwin_arm64
omc_Darwin_arm64.tar.gz
omc_Darwin_amd64
omc_Darwin_aarch64.tar.gz
omc_Windows_x86_64.zip
name: "${{ env.RELEASE_TAG }} release"
41 changes: 1 addition & 40 deletions cmd/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,10 @@ var GetCmd = &cobra.Command{
klog.V(1).ErrorS(err, "ERROR")
os.Exit(1)
}
// namespaces, clusterloggings and clusterlogforwarders locations
// namespaces and projects resources
// are exceptions to must-gather resources structure
if resourceNamePlural == "namespaces" || resourceNamePlural == "projects" {
getNamespacesResources(resourceNamePlural, resourceGroup, vars.GetArgs[resourceNamePlural+"."+resourceGroup])
} else if resourceNamePlural == "clusterloggings" {
getClusterLoggingResources(resourceNamePlural, resourceGroup, vars.GetArgs[resourceNamePlural+"."+resourceGroup])
} else if resourceNamePlural == "clusterlogforwarders" {
getClusterLogForwarderResources(resourceNamePlural, resourceGroup, vars.GetArgs[resourceNamePlural+"."+resourceGroup])
} else if resourceNamePlural == "podnetworkconnectivitychecks" {
getPodNetworkConnectivityChecksResources(resourceNamePlural, resourceGroup, vars.GetArgs[resourceNamePlural+"."+resourceGroup])
} else if namespaced {
Expand Down Expand Up @@ -273,41 +269,6 @@ func getNamespacedResources(resourceNamePlural string, resourceGroup string, res
}
}

func getClusterLogForwarderResources(resourceNamePlural string, resourceGroup string, resources map[string]struct{}) {
resourcesYamlPath := vars.MustGatherRootPath + "/cluster-logging/clo/clusterlogforwarder_instance.yaml"
_file, err := ioutil.ReadFile(resourcesYamlPath)
if err == nil {
UnstructuredItems := types.UnstructuredList{ApiVersion: "v1", Kind: "List"}
if err := yaml.Unmarshal(_file, &UnstructuredItems); err != nil {
fmt.Fprintln(os.Stderr, "Error when trying to unmarshal file: "+resourcesYamlPath)
os.Exit(1)
}
for _, item := range UnstructuredItems.Items {
_, ok := resources[item.GetName()]
if ok || len(resources) == 0 {
handleObject(item)
}
}
}
}

func getClusterLoggingResources(resourceNamePlural string, resourceGroup string, resources map[string]struct{}) {
resourcesYamlPath := vars.MustGatherRootPath + "/cluster-logging/clo/clusterlogging_instance.yaml"
_file, err := ioutil.ReadFile(resourcesYamlPath)
if err == nil {
UnstructuredItems := types.UnstructuredList{ApiVersion: "v1", Kind: "List"}
if err := yaml.Unmarshal(_file, &UnstructuredItems); err != nil {
fmt.Fprintln(os.Stderr, "Error when trying to unmarshal file: "+resourcesYamlPath)
os.Exit(1)
}
for _, item := range UnstructuredItems.Items {
_, ok := resources[item.GetName()]
if ok || len(resources) == 0 {
handleObject(item)
}
}
}
}

func getNamespacesResources(resourceNamePlural string, resourceGroup string, resources map[string]struct{}) {
if len(resources) > 0 {
Expand Down
30 changes: 0 additions & 30 deletions cmd/get/known-resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -203,36 +203,6 @@ ns:
name: namespace
namespaced: false
plural: namespaces
clf:
group: logging.openshift.io
name: clusterlogforwarder
namespaced: false
plural: clusterlogforwarders
clusterlogforwarder:
group: logging.openshift.io
name: clusterlogforwarder
namespaced: false
plural: clusterlogforwarders
clusterlogforwarders:
group: logging.openshift.io
name: clusterlogforwarder
namespaced: true
plural: clusterlogforwarders
cl:
group: logging.openshift.io
name: clusterlogging
namespaced: false
plural: clusterloggings
clusterlogging:
group: logging.openshift.io
name: clusterlogging
namespaced: false
plural: clusterloggings
clusterloggings:
group: logging.openshift.io
name: clusterlogging
namespaced: true
plural: clusterloggings
node:
group: core
name: node
Expand Down
2 changes: 1 addition & 1 deletion cmd/haproxy/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func newBackendFromLine(raw []string) *backend {
}

func isBackendBlock(line string) *backend {
backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_]*)$`
backendRe := `^backend ([a-z0-9\-\_]*):([a-z0-9\-\_]*):([a-z0-9\-\_\.]*)$`
re := regexp.MustCompile(backendRe)

matches := re.FindStringSubmatch(line)
Expand Down
12 changes: 11 additions & 1 deletion cmd/haproxy/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestParseHAProxyConfig(t *testing.T) {
includeOpenShiftNamespaces: false,
expected: []*backend{
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"},
&backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}},
},
{
Expand All @@ -45,14 +46,17 @@ func TestParseHAProxyConfig(t *testing.T) {
expected: []*backend{
&backend{namespace: "openshift-monitoring", routeName: "thanos-querier", ingressController: "default", service: &service{serviceName: "thanos-querier", port: &port{portNr: 9091, portName: "web"}}, termination: "be_secure"},
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"},
&backend{namespace: "other-testdata", routeName: "hello-node-secure", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_edge_http"}},
},
{
name: "Parse HAProxy config and extract backends matching a namespace",
configFile: "../../testdata/ingress_controllers/default/router-default-abc123-a1b1c3/haproxy.config",
wantedNamespace: "testdata",
includeOpenShiftNamespaces: true,
expected: []*backend{&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"}},
expected: []*backend{
&backend{namespace: "testdata", routeName: "rails-postgresql-example", ingressController: "default", service: &service{serviceName: "rails-postgresql-example", port: &port{portNr: 8080, portName: "web"}}, termination: "be_http"},
&backend{namespace: "testdata", routeName: "app.example.com", ingressController: "default", service: &service{serviceName: "hello-node", port: &port{portNr: 8080, portName: ""}}, termination: "be_http"}},
},
}

Expand Down Expand Up @@ -134,6 +138,12 @@ func TestIsBackendBlock(t *testing.T) {
includeOpenShift: true,
expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node", ingressController: "", service: (*service)(nil)},
},
{
name: "return backend from valid backend block including dot in route",
line: "backend be_edge_http:testdata:hello-node.example.com",
includeOpenShift: true,
expected: &backend{termination: "be_edge_http", namespace: "testdata", routeName: "hello-node.example.com", ingressController: "", service: (*service)(nil)},
},
{
name: "return nil from invalid backend block",
line: "nonbackend be_edge_http:testdata:hello-node",
Expand Down
22 changes: 19 additions & 3 deletions cmd/logs/logreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
)

// logReader reads (current/previous/rotated) logs from a tree in a base directory:
Expand Down Expand Up @@ -63,7 +64,19 @@ func (l *LogReader) WithFilter(llf logLineFilter) {
}

func (l *LogReader) FromPrevious() {
l.files = &[]string{previousLogFile, previousInsecureLogFile}
l.files = &[]string{previousLogFile}
}

func (l *LogReader) FromInsecure() {
// iterate selected logs and read from its insecure siblings instead
insecure := []string{}
for _, f := range *l.files {
nf := strings.TrimSuffix(f, ".log")
if f != nf {
insecure = append(insecure, nf+".insecure.log")
}
}
*l.files = insecure
}

func (l *LogReader) FromRotated() {
Expand All @@ -77,7 +90,10 @@ func (l *LogReader) Read(w io.Writer) {
for _, filename := range *l.files {
reader, err := open(l.dirname + "/" + filename)
if err != nil {
fmt.Println(err)
if !os.IsNotExist(err) {
// since we're scanning through logs dynamically don't error out if log files do not exist
fmt.Errorf("failed to open log file: %v", err)
}
continue
}
defer reader.Close()
Expand Down Expand Up @@ -117,7 +133,7 @@ func (l *LogReader) applyFilter(raw []byte) []byte {
func open(filename string) (io.ReadCloser, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open log file: %v", err)
return nil, err
}
var reader io.ReadCloser
reader, err = gzip.NewReader(file)
Expand Down
31 changes: 30 additions & 1 deletion cmd/logs/logreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ func TestRotatedFiles(t *testing.T) {
}
}

func TestFromInsecure(t *testing.T) {
tests := []struct {
name string
logReader *LogReader
expected *[]string
}{
{
name: "Handle insecure logs given a list of requested log files",
logReader: NewLogReader(""), // defaults to current.log
expected: &[]string{"current.insecure.log"},
},
{
name: "Handle insecure logs but only touch files suffixed .log",
logReader: &LogReader{"", &[]string{"current.log", "current.fakelog"}, nil},
expected: &[]string{"current.insecure.log"},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.logReader.FromInsecure()

if !reflect.DeepEqual(tc.logReader.files, tc.expected) {
t.Fatalf("Expected : %v, got: %v", tc.expected, tc.logReader.files)
}
})
}
}

func TestOpen(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -86,7 +115,7 @@ func TestOpen(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := open(tc.fixture)
if err == nil && tc.expectedError {
if tc.expectedError && err == nil {
t.Errorf("Expected error, got %v", got)
}
expectedType := reflect.TypeOf(tc.expected)
Expand Down
9 changes: 5 additions & 4 deletions cmd/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var Logs = &cobra.Command{
containerName, _ := cmd.Flags().GetString("container")
previousFlag, _ := cmd.Flags().GetBool("previous")
rotatedFlag, _ := cmd.Flags().GetBool("rotated")
insecureFlag, _ := cmd.Flags().GetBool("insecure")
allContainersFlag, _ := cmd.Flags().GetBool("all-containers")
logLevels := []string{}
if LogLevel != "" {
Expand All @@ -85,10 +86,10 @@ var Logs = &cobra.Command{
fmt.Fprintln(os.Stderr, "arguments in resource/name form must have a single resource and name")
os.Exit(1)
}
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
} else {
podName = s[0]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
}
if len(args) == 2 {
Expand All @@ -103,7 +104,7 @@ var Logs = &cobra.Command{
os.Exit(1)
}
containerName = args[1]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
} else {
if containerName != "" {
Expand All @@ -112,7 +113,7 @@ var Logs = &cobra.Command{
} else {
podName = args[0]
containerName = args[1]
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels)
logsPods(vars.MustGatherRootPath, vars.Namespace, podName, containerName, previousFlag, rotatedFlag, allContainersFlag, logLevels, insecureFlag)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/logs/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"sigs.k8s.io/yaml"
)

func logsPods(currentContextPath string, defaultConfigNamespace string, podName string, containerName string, previousFlag bool, rotatedFlag bool, allContainersFlag bool, logLevels []string) {
func logsPods(currentContextPath string, defaultConfigNamespace string, podName string, containerName string, previousFlag bool, rotatedFlag bool, allContainersFlag bool, logLevels []string, insecureFlag bool) {
var logFilter logLineFilter = NewCRILogFilter(logLevels, nil)
var _Items v1.PodList
CurrentNamespacePath := currentContextPath + "/namespaces/" + defaultConfigNamespace
Expand Down Expand Up @@ -70,6 +70,9 @@ func logsPods(currentContextPath string, defaultConfigNamespace string, podName
if rotatedFlag {
log.FromRotated()
}
if insecureFlag {
log.FromInsecure()
}
log.Read(os.Stdout)
}
return
Expand Down Expand Up @@ -111,6 +114,9 @@ func logsPods(currentContextPath string, defaultConfigNamespace string, podName
if rotatedFlag {
log.FromRotated()
}
if insecureFlag {
log.FromInsecure()
}
log.Read(os.Stdout)
}
}
Expand Down
19 changes: 15 additions & 4 deletions cmd/upgrade/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ func updateOmcExecutable(omcExecutablePath string, url string, desiredVersion st
}
defer resp.Body.Close()

err = os.Remove(omcExecutablePath)
tempFile, err := os.CreateTemp("", "omcExecutable_*.tmp")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return err
}
defer tempFile.Close()

err = tempFile.Chmod(0755)
if err != nil {
return err
}
f, err := os.OpenFile(omcExecutablePath, os.O_CREATE|os.O_WRONLY, 0777)

f, err := os.OpenFile(tempFile.Name(), os.O_CREATE|os.O_WRONLY, 0777)
if err != nil {
return err
}
Expand All @@ -68,6 +74,11 @@ func updateOmcExecutable(omcExecutablePath string, url string, desiredVersion st
if err != nil {
return err
}

err = os.Rename(tempFile.Name(), omcExecutablePath)
if err != nil {
return err
}
return nil
}

Expand Down
Loading

0 comments on commit 9e356ba

Please sign in to comment.