Skip to content

Commit

Permalink
Fix sonar
Browse files Browse the repository at this point in the history
Signed-off-by: Omar Farag <[email protected]>
  • Loading branch information
o-farag committed Jul 28, 2023
1 parent 446e280 commit 4f2d50d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 47 deletions.
3 changes: 2 additions & 1 deletion pkg/install/expectedArgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/install/hypershift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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"]
Expand Down
77 changes: 33 additions & 44 deletions pkg/install/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -225,53 +226,26 @@ 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
}

}

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 {
Expand All @@ -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
}

0 comments on commit 4f2d50d

Please sign in to comment.