Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to set target cluster in pkg install cmd #1221

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 31 additions & 23 deletions cli/pkg/kctrl/cmd/package/installed/create_or_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type CreateOrUpdateOptions struct {
valuesFile string
values bool
serviceAccountName string
targetCluster string

install bool

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down