From 4f2d50dc18805709f7a572d22a9d590242dbfd7e Mon Sep 17 00:00:00 2001 From: Omar Farag Date: Fri, 28 Jul 2023 01:08:11 -0400 Subject: [PATCH] Fix sonar Signed-off-by: Omar Farag --- pkg/install/expectedArgs.go | 3 +- pkg/install/hypershift.go | 6 ++- pkg/install/upgrade.go | 77 ++++++++++++++++--------------------- 3 files changed, 39 insertions(+), 47 deletions(-) diff --git a/pkg/install/expectedArgs.go b/pkg/install/expectedArgs.go index 0a740204..9eba1b77 100644 --- a/pkg/install/expectedArgs.go +++ b/pkg/install/expectedArgs.go @@ -36,7 +36,8 @@ func (c *UpgradeController) getDeployment(operatorName string) (appsv1.Deploymen } // Match {text} and remove it -// Returns matched text e.g. --oidc-storage-provider-s3-bucket-name={bucket} will become "--oidc-storage-provider-s3-bucket-name=" and return "bucket" +// Returns matched text e.g. --oidc-storage-provider-s3-bucket-name={bucket} will become +// "--oidc-storage-provider-s3-bucket-name=" and return "bucket" func matchAndTrim(s *string) string { i := strings.Index(*s, "{") if i >= 0 { diff --git a/pkg/install/hypershift.go b/pkg/install/hypershift.go index a41b1f6a..86eba315 100644 --- a/pkg/install/hypershift.go +++ b/pkg/install/hypershift.go @@ -311,7 +311,8 @@ func (c *UpgradeController) runHypershiftInstall(ctx context.Context, controller // compare installed operator images to the new image stream // If they are the same, skip re-install. if reinstallCheckRequired && - !(c.operatorImagesUpdated(im, *operatorDeployment) || c.configmapDataUpdated(util.HypershiftInstallFlagsCM, installFlagsCM)) { + !(c.operatorImagesUpdated(im, *operatorDeployment) || + c.configmapDataUpdated(util.HypershiftInstallFlagsCM, installFlagsCM)) { c.log.Info("no change in hypershift operator images and install flags, skipping hypershift operator installation") return nil } @@ -471,7 +472,8 @@ func getParamValue(s []string, e string) string { return "" } -func (c *UpgradeController) createOrUpdateAwsSpokeSecret(ctx context.Context, hubSecret *corev1.Secret, regionRequired bool) error { +func (c *UpgradeController) createOrUpdateAwsSpokeSecret( + ctx context.Context, hubSecret *corev1.Secret, regionRequired bool) error { spokeSecret := hubSecret.DeepCopy() region := hubSecret.Data["region"] diff --git a/pkg/install/upgrade.go b/pkg/install/upgrade.go index 218794f7..be0d1a9a 100644 --- a/pkg/install/upgrade.go +++ b/pkg/install/upgrade.go @@ -114,12 +114,14 @@ func (c *UpgradeController) installOptionsChanged() bool { objectArgs: []expectedArg{ {argument: "--oidc-storage-provider-s3-bucket-name={bucket}", shouldExist: true}, {argument: "--oidc-storage-provider-s3-region={region}", shouldExist: true}, - {argument: "--oidc-storage-provider-s3-credentials=/etc/oidc-storage-provider-s3-creds/credentials", shouldExist: true}, + {argument: "--oidc-storage-provider-s3-credentials=/etc/oidc-storage-provider-s3-creds/credentials", + shouldExist: true}, }, NoObjectArgs: []expectedArg{ {argument: "--oidc-storage-provider-s3-bucket-name=", shouldExist: false}, {argument: "--oidc-storage-provider-s3-region=", shouldExist: false}, - {argument: "--oidc-storage-provider-s3-credentials=/etc/oidc-storage-provider-s3-creds/credentials", shouldExist: false}, + {argument: "--oidc-storage-provider-s3-credentials=/etc/oidc-storage-provider-s3-creds/credentials", + shouldExist: false}, }, deploymentName: util.HypershiftOperatorName, }, @@ -160,18 +162,17 @@ func (c *UpgradeController) installOptionsChanged() bool { deploymentArgs := dep.Spec.Template.Spec.Containers[0].Args - if err := c.hubClient.Get(context.TODO(), types.NamespacedName{Name: o.objectName, Namespace: c.clusterName}, &corev1.Secret{}); err == nil { + if err := c.hubClient.Get( + context.TODO(), types.NamespacedName{Name: o.objectName, Namespace: c.clusterName}, + &corev1.Secret{}); err == nil { + if argMismatch(o.objectArgs, deploymentArgs) { - fmt.Println(deploymentArgs) c.log.Info(fmt.Sprintf("Mismatch between %s args and install options", o.objectName)) - fmt.Println(o.objectArgs) return true } } else { if argMismatch(o.NoObjectArgs, deploymentArgs) { - fmt.Println(deploymentArgs) c.log.Info(fmt.Sprintf("Mismatch between %s args and install options", o.objectName)) - fmt.Println(o.objectArgs) return true } } @@ -225,45 +226,18 @@ func (c *UpgradeController) configmapDataChanged(oldCM, newCM corev1.ConfigMap, func (c *UpgradeController) syncHypershiftNS() error { //Sync secrets in local-cluster namespace with secrets in hypershift namespace - secrets := []string{"hypershift-operator-oidc-provider-s3-credentials", "hypershift-operator-private-link-credentials", "hypershift-operator-external-dns-credentials"} - awsPlatform := false + secrets := []string{util.HypershiftBucketSecretName, + util.HypershiftPrivateLinkSecretName, + util.HypershiftExternalDNSSecretName} ctx := context.TODO() for s := range secrets { - if secrets[s] == "hypershift-operator-external-dns-credentials" { - - extDNSSecretKey := types.NamespacedName{Name: util.HypershiftExternalDNSSecretName, Namespace: c.clusterName} - sExtDNS := &corev1.Secret{} - if err := c.hubClient.Get(ctx, extDNSSecretKey, sExtDNS); err == nil { - if awsPlatform { - // For AWS DNS provider, users can specify either credentials or - // aws-access-key-id and aws-secret-access-key - if err := c.createOrUpdateAwsSpokeSecret(ctx, sExtDNS, false); err != nil { - return err - } - } else { - if err := c.createOrUpdateSpokeSecret(ctx, sExtDNS); err != nil { - return err - } - } - } else { - c.log.Info(fmt.Sprintf("external dns secret(%s) was not found", extDNSSecretKey)) - } - - } else { - - secretKey := types.NamespacedName{Name: secrets[s], Namespace: c.clusterName} - se := &corev1.Secret{} - if err := c.hubClient.Get(ctx, secretKey, se); err == nil { - awsPlatform = true - if err := c.createOrUpdateAwsSpokeSecret(ctx, se, true); err != nil { - return err - } - } else { - c.log.Info(fmt.Sprintf("secret(%s) not found on the hub.", secretKey)) - - } - + secretKey := types.NamespacedName{Name: secrets[s], Namespace: c.clusterName} + se := &corev1.Secret{} + if err := c.hubClient.Get(ctx, secretKey, se); err != nil { + c.log.Info(fmt.Sprintf("secret(%s) not found on the hub.", secretKey)) + } else if err := c.createOrUpdateSecret(ctx, se); err != nil { + return err } } @@ -271,7 +245,7 @@ func (c *UpgradeController) syncHypershiftNS() error { return nil } -func (c *UpgradeController) populateExpectedArgs(toPopulate *[]expectedConfig) error { +func (c *UpgradeController) populateExpectedArgs(toPopulate *[]expectedConfig) { //anything with {key} gets replaced with the value of 'key' in the secret tp := *toPopulate for e := range tp { @@ -297,5 +271,20 @@ func (c *UpgradeController) populateExpectedArgs(toPopulate *[]expectedConfig) e } } +} + +func (c *UpgradeController) createOrUpdateSecret(ctx context.Context, secret *corev1.Secret) error { + if secret.Name == util.HypershiftExternalDNSSecretName && !c.awsPlatform { + if err := c.createOrUpdateSpokeSecret(ctx, secret); err != nil { + return err + } + } else { + c.awsPlatform = true + if err := c.createOrUpdateAwsSpokeSecret(ctx, secret, + secret.Name != util.HypershiftExternalDNSSecretName); err != nil { + return err + } + + } return nil }