From 316a695bfdabce7e292fc112ea5a2ed07e8c98ce Mon Sep 17 00:00:00 2001 From: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> Date: Tue, 30 May 2023 02:23:31 +0530 Subject: [PATCH] Add option to set target cluster in pkg install cmd Signed-off-by: Praveen Rewar <8457124+praveenrewar@users.noreply.github.com> --- .../cmd/package/installed/create_or_update.go | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go index 15b7af035..8177a0ef3 100644 --- a/cli/pkg/kctrl/cmd/package/installed/create_or_update.go +++ b/cli/pkg/kctrl/cmd/package/installed/create_or_update.go @@ -51,6 +51,7 @@ type CreateOrUpdateOptions struct { valuesFile string values bool serviceAccountName string + targetCluster string install bool @@ -103,6 +104,7 @@ func NewCreateCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) * cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional") cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional") cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional") + cmd.Flags().StringVar(&o.targetCluster, "target", "", "Set target cluster, optional") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, @@ -149,6 +151,7 @@ func NewInstallCmd(o *CreateOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) cmd.Flags().StringVar(&o.valuesFile, "values-file", "", "The path to the configuration values file, optional") cmd.Flags().BoolVar(&o.values, "values", true, "Add or keep values supplied to package install, optional") cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional") + cmd.Flags().StringVar(&o.targetCluster, "target", "", "Set target cluster, optional") o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{ AllowDisableWait: true, @@ -532,23 +535,8 @@ func (o *CreateOrUpdateOptions) createRelatedResources(client kubernetes.Interfa err error ) - if o.serviceAccountName == "" { - - o.statusUI.PrintMessagef("Creating service account '%s'", o.createdAnnotations.ServiceAccountAnnValue()) - if isServiceAccountCreated, err = o.createOrUpdateServiceAccount(client); err != nil { - return isServiceAccountCreated, isSecretCreated, err - } - - o.statusUI.PrintMessagef("Creating cluster admin role '%s'", o.createdAnnotations.ClusterRoleAnnValue()) - if err := o.createOrUpdateClusterAdminRole(client); err != nil { - return isServiceAccountCreated, isSecretCreated, err - } - - o.statusUI.PrintMessagef("Creating cluster role binding '%s'", o.createdAnnotations.ClusterRoleBindingAnnValue()) - if err := o.createOrUpdateClusterRoleBinding(client); err != nil { - return isServiceAccountCreated, isSecretCreated, err - } - } else { + switch { + case o.serviceAccountName != "": client, err := o.depsFactory.CoreClient() if err != nil { return isServiceAccountCreated, isSecretCreated, err @@ -572,6 +560,23 @@ func (o *CreateOrUpdateOptions) createRelatedResources(client kubernetes.Interfa return isServiceAccountCreated, isSecretCreated, err } } + case o.targetCluster != "": + // do nothing, kubeconfig secret is provided + default: + o.statusUI.PrintMessagef("Creating service account '%s'", o.createdAnnotations.ServiceAccountAnnValue()) + if isServiceAccountCreated, err = o.createOrUpdateServiceAccount(client); err != nil { + return isServiceAccountCreated, isSecretCreated, err + } + + o.statusUI.PrintMessagef("Creating cluster admin role '%s'", o.createdAnnotations.ClusterRoleAnnValue()) + if err := o.createOrUpdateClusterAdminRole(client); err != nil { + return isServiceAccountCreated, isSecretCreated, err + } + + o.statusUI.PrintMessagef("Creating cluster role binding '%s'", o.createdAnnotations.ClusterRoleBindingAnnValue()) + if err := o.createOrUpdateClusterRoleBinding(client); err != nil { + return isServiceAccountCreated, isSecretCreated, err + } } if o.valuesFile != "" && o.values { @@ -687,16 +692,10 @@ func (o *CreateOrUpdateOptions) createOrUpdateDataValuesSecret(client kubernetes } func (o *CreateOrUpdateOptions) createPackageInstall(serviceAccountCreated, secretCreated bool, overlaysSecretName string, kcClient kcclient.Interface) error { - svcAccount := o.serviceAccountName - if svcAccount == "" { - svcAccount = o.createdAnnotations.ServiceAccountAnnValue() - } - // construct the PackageInstall CR packageInstall := &kcpkgv1alpha1.PackageInstall{ ObjectMeta: metav1.ObjectMeta{Name: o.Name, Namespace: o.NamespaceFlags.Name}, Spec: kcpkgv1alpha1.PackageInstallSpec{ - ServiceAccountName: svcAccount, PackageRef: &kcpkgv1alpha1.PackageRef{ RefName: o.packageName, VersionSelection: &versions.VersionSelectionSemver{ @@ -707,6 +706,15 @@ func (o *CreateOrUpdateOptions) createPackageInstall(serviceAccountCreated, secr }, } + switch { + case o.serviceAccountName != "": + packageInstall.Spec.ServiceAccountName = o.serviceAccountName + case o.targetCluster != "": + packageInstall.Spec.Cluster = &kcv1alpha1.AppCluster{KubeconfigSecretRef: &kcv1alpha1.AppClusterKubeconfigSecretRef{Name: o.targetCluster}} + default: + packageInstall.Spec.ServiceAccountName = o.createdAnnotations.ServiceAccountAnnValue() + } + // if configuration data file was provided, reference the secret name in the PackageInstall if secretCreated { packageInstall.Spec.Values = []kcpkgv1alpha1.PackageInstallValues{