Skip to content

Commit 15c92de

Browse files
authored
kctrl: Flag to create namespace when adding new repo (#1113)
* kctrl: Flag to create namespace when adding repo When adding a new package repository to a cluster, it's now possible to create the installation namespace automatically by specifying the "--create-namespace" flag. Fixes gh-1001 Signed-off-by: Thomas Vitale <[email protected]> * Improve error handling Signed-off-by: Thomas Vitale <[email protected]> * Optimize status messages for namespace creation Signed-off-by: Thomas Vitale <[email protected]> * Add cleanup after tests Signed-off-by: Thomas Vitale <[email protected]> * Update test cleanup Signed-off-by: Thomas Vitale <[email protected]> * Use cleanup function for new tests Signed-off-by: Thomas Vitale <[email protected]> * Re-use existing namespace in test Signed-off-by: Thomas Vitale <[email protected]> --------- Signed-off-by: Thomas Vitale <[email protected]>
1 parent edac0f1 commit 15c92de

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

cli/pkg/kctrl/cmd/package/repository/add_or_update.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
kcpkg "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
2020
kcclient "github.com/vmware-tanzu/carvel-kapp-controller/pkg/client/clientset/versioned"
2121
versions "github.com/vmware-tanzu/carvel-vendir/pkg/vendir/versions/v1alpha1"
22+
corev1 "k8s.io/api/core/v1"
2223
"k8s.io/apimachinery/pkg/api/errors"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"sigs.k8s.io/yaml"
@@ -34,6 +35,7 @@ type AddOrUpdateOptions struct {
3435
SecureNamespaceFlags cmdcore.SecureNamespaceFlags
3536
Name string
3637
URL string
38+
CreateNamespace bool
3739

3840
DryRun bool
3941

@@ -76,6 +78,8 @@ func NewAddCmd(o *AddOrUpdateOptions, flagsFactory cmdcore.FlagsFactory) *cobra.
7678
cmd.Flags().StringVar(&o.URL, "url", "", "OCI registry url for package repository bundle (required)")
7779
cmd.Flags().BoolVar(&o.DryRun, "dry-run", false, "Print YAML for resources being applied to the cluster without applying them, optional")
7880

81+
cmd.Flags().BoolVar(&o.CreateNamespace, "create-namespace", false, "Create the package repository namespace if not present (default false)")
82+
7983
o.WaitFlags.Set(cmd, flagsFactory, &cmdcore.WaitFlagsOpts{
8084
AllowDisableWait: true,
8185
DefaultInterval: 1 * time.Second,
@@ -153,6 +157,28 @@ func (o *AddOrUpdateOptions) Run(args []string) error {
153157
return err
154158
}
155159

160+
if o.CreateNamespace {
161+
coreClient, err := o.depsFactory.CoreClient()
162+
if err != nil {
163+
return err
164+
}
165+
166+
namespace := &corev1.Namespace{
167+
ObjectMeta: metav1.ObjectMeta{
168+
Name: o.NamespaceFlags.Name,
169+
},
170+
}
171+
172+
_, err = coreClient.CoreV1().Namespaces().Create(context.Background(), namespace, metav1.CreateOptions{})
173+
if err != nil {
174+
if !errors.IsAlreadyExists(err) {
175+
return err
176+
}
177+
} else {
178+
o.statusUI.PrintMessagef("Created namespace '%s'", o.NamespaceFlags.Name)
179+
}
180+
}
181+
156182
existingRepository, err := client.PackagingV1alpha1().PackageRepositories(o.NamespaceFlags.Name).Get(
157183
context.Background(), o.Name, metav1.GetOptions{})
158184
if err != nil {
@@ -174,7 +200,6 @@ func (o *AddOrUpdateOptions) Run(args []string) error {
174200
}
175201

176202
if o.WaitFlags.Enabled {
177-
o.ui.PrintLinef("Waiting for package repository to be updated")
178203
err = o.waitForPackageRepositoryInstallation(client)
179204
}
180205

cli/test/e2e/package_repository_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ func TestPackageRepository(t *testing.T) {
2020
pkgrName := "test-package-repository"
2121
pkgrURL := `index.docker.io/k8slt/kc-e2e-test-repo:latest`
2222

23+
newRepoNamespace := "carvel-test-repo-a"
24+
2325
kind := "PackageRepository"
2426

2527
cleanUp := func() {
2628
RemoveClusterResource(t, kind, pkgrName, env.Namespace, kubectl)
29+
RemoveClusterResource(t, kind, pkgrName, newRepoNamespace, kubectl)
2730
}
2831

2932
cleanUp()
@@ -127,4 +130,16 @@ func TestPackageRepository(t *testing.T) {
127130
require.Exactly(t, expectedOutputRows, output.Tables[0].Rows)
128131
})
129132

133+
logger.Section("creating a repository in a new namespace that doesn't exist", func() {
134+
kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL, "-n", newRepoNamespace, "--create-namespace"})
135+
136+
kubectl.Run([]string{"get", kind, pkgrName, "-n", newRepoNamespace})
137+
})
138+
139+
logger.Section("creating a repository in a namespace that already exists", func() {
140+
kappCtrl.Run([]string{"package", "repository", "add", "-r", pkgrName, "--url", pkgrURL, "-n", env.Namespace, "--create-namespace"})
141+
142+
kubectl.Run([]string{"get", kind, pkgrName, "-n", env.Namespace})
143+
})
144+
130145
}

0 commit comments

Comments
 (0)