Skip to content

Commit

Permalink
bug-fix-global-no-ns-01 (#25)
Browse files Browse the repository at this point in the history
* bug-fix-global-no-ns-01

* bug-fix-global-no-ns-01; for non listTypes := []string{deployment, ingress, service, sa, configmap, secret, role, rolebinding, hpa, cronjob}
  • Loading branch information
aguzmans authored Feb 22, 2024
1 parent 4a59e42 commit 7d389f3
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ func main() {
// Parse CLI arguments
args := cli.PaserReader()
if args.Err != nil {
err := fmt.Errorf("Error parsing arguments: %v", args.Err)
err := fmt.Errorf("error parsing arguments: %v", args.Err)
panic(err)
}

// Connect to source cluster
clientsetToSource, err := connect.ConnectToSource(args.SourceClusterContext, &args.KubeconfigFile)
if err != nil {
err = fmt.Errorf("Error connecting to source cluster: %v\n", err)
err = fmt.Errorf("error connecting to source cluster: %v\n", err)
panic(err)
}

// Connect to target cluster
clientsetToTarget, err := connect.ContextSwitch(args.TargetClusterContext, &args.KubeconfigFile)
if err != nil {
err = fmt.Errorf("Error switching context: %v\n", err)
err = fmt.Errorf("error switching context: %v\n", err)
panic(err)
}

Expand All @@ -47,22 +47,22 @@ func main() {
fmt.Println("Using", args.NamespaceName, "namespace")
sourceNameSpace, err = query.GetNamespace(clientsetToSource, args.NamespaceName)
if err != nil {
err = fmt.Errorf("Error listing namespaces: %v\n", err)
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
sourceNameSpacesList = &v1.NamespaceList{Items: []v1.Namespace{*sourceNameSpace}}
case "wildcard":
sourceNameSpacesList, err = query.ListNameSpaces(clientsetToSource)
if err != nil {
err = fmt.Errorf("Error listing namespaces: %v\n", err)
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
sourceNameSpacesList = filterNamespaces(sourceNameSpacesList, args.NamespaceName)
case "empty":
iterateGoglabObjects(clientsetToSource, clientsetToTarget, args)
sourceNameSpacesList, err = query.ListNameSpaces(clientsetToSource)
if err != nil {
err = fmt.Errorf("Error listing namespaces: %v\n", err)
err = fmt.Errorf("error listing namespaces: %v", err)
panic(err)
}
}
Expand All @@ -86,25 +86,25 @@ func iterateGoglabObjects(clientsetToSource, clientsetToTarget *kubernetes.Clien
case "namespace":
_, err := compare.CompareNameSpaces(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Namespaces: %v", err)
err = fmt.Errorf("error comparing Namespaces: %v", err)
panic(err)
}
case "crd":
_, err := compare.CompareCRDs(args.TargetClusterContext, args.KubeconfigFile, args)
if err != nil {
err = fmt.Errorf("Error comparing CRDs: %v", err)
err = fmt.Errorf("error comparing CRDs: %v", err)
panic(err)
}
case "clusterrole":
_, err := compare.CompareClusterRoles(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Role: %v", err)
err = fmt.Errorf("error comparing Cluster Role: %v", err)
panic(err)
}
case "clusterrolebinding":
_, err := compare.CompareClusterRoleBindings(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Role: %v", err)
err = fmt.Errorf("error comparing Cluster Role: %v", err)
panic(err)
}
}
Expand Down Expand Up @@ -153,22 +153,22 @@ func iterateGoglabObjects(clientsetToSource, clientsetToTarget *kubernetes.Clien
if args.Include == nil && args.Exclude == nil {
_, err := compare.CompareNameSpaces(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Namespaces: %v", err)
err = fmt.Errorf("error comparing Namespaces: %v", err)
panic(err)
}
_, err = compare.CompareCRDs(args.TargetClusterContext, args.KubeconfigFile, args)
if err != nil {
err = fmt.Errorf("Error comparing CRDs: %v", err)
err = fmt.Errorf("error comparing CRDs: %v", err)
panic(err)
}
_, err = compare.CompareClusterRoles(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Roles: %v", err)
err = fmt.Errorf("error comparing Cluster Roles: %v", err)
panic(err)
}
_, err = compare.CompareClusterRoleBindings(clientsetToSource, clientsetToTarget, args)
if err != nil {
err = fmt.Errorf("Error comparing Cluster Role Bindings: %v", err)
err = fmt.Errorf("error comparing Cluster Role Bindings: %v", err)
panic(err)
}
comparisonPerformed = true
Expand Down Expand Up @@ -239,61 +239,61 @@ func compareResource(clientsetToSource, clientsetToTarget *kubernetes.Clientset,
case "deployment":
_, err := compare.CompareDeployments(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Deployments: %v", err)
err = fmt.Errorf("error comparing Deployments: %v", err)
panic(err)
}
case "ingress":
_, err := compare.CompareIngresses(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Ingresses: %v", err)
err = fmt.Errorf("error comparing Ingresses: %v", err)
panic(err)
}
case "service":
_, err := compare.CompareServices(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Services: %v", err)
err = fmt.Errorf("error comparing Services: %v", err)
panic(err)
}
case "sa":
_, err := compare.CompareServiceAccounts(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Service Accounts: %v", err)
err = fmt.Errorf("error comparing Service Accounts: %v", err)
panic(err)
}
case "configmap":
_, err := compare.CompareConfigMaps(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Config Maps: %v", err)
err = fmt.Errorf("error comparing Config Maps: %v", err)
panic(err)
}
case "secret":
_, err := compare.CompareSecrets(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Secrets: %v", err)
err = fmt.Errorf("error comparing Secrets: %v", err)
panic(err)
}
case "role":
_, err := compare.CompareRoles(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Roles: %v", err)
err = fmt.Errorf("error comparing Roles: %v", err)
panic(err)
}
case "rolebinding":
_, err := compare.CompareRoleBindings(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Role Bindings: %v", err)
err = fmt.Errorf("error comparing Role Bindings: %v", err)
panic(err)
}
case "hpa":
_, err := compare.CompareHPAs(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Horizontal Pod Autoscalers: %v", err)
err = fmt.Errorf("error comparing Horizontal Pod Autoscalers: %v", err)
panic(err)
}
case "cronjob":
_, err := compare.CompareCronJobs(clientsetToSource, clientsetToTarget, namespace, TheArgs)
if err != nil {
err = fmt.Errorf("Error comparing Cron Jobs: %v", err)
err = fmt.Errorf("error comparing Cron Jobs: %v", err)
panic(err)
}
}
Expand All @@ -308,8 +308,13 @@ func iterateNamespaces(sourceNameSpacesList *v1.NamespaceList, clientsetToSource
}
} else {
// Compare resources based on include or exclude lists
for _, ns := range sourceNameSpacesList.Items {
compareResourcesByLists(clientsetToSource, clientsetToTarget, ns.Name, TheArgs)
resources := []string{"deployment", "ingress", "service", "serviceaccount", "configmap", "secret", "role", "rolebinding", "hpa", "cronjob"}
if tools.AreAnyInLists(TheArgs.Include, resources) || tools.AreAnyInLists(TheArgs.Exclude, resources) {
for _, ns := range sourceNameSpacesList.Items {
compareResourcesByLists(clientsetToSource, clientsetToTarget, ns.Name, TheArgs)
}
} else {
fmt.Println("No namespaced resources to compare")
}
}
}
Expand Down

0 comments on commit 7d389f3

Please sign in to comment.