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

KEP-31: Impl of --create-namespace #1531

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions config/crds/kudo.dev_operators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spec:
type: string
type: object
type: array
namespaceManifest:
type: string
url:
type: string
type: object
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/kudo/v1beta1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type OperatorSpec struct {
KubernetesVersion string `json:"kubernetesVersion,omitempty"`
Maintainers []*Maintainer `json:"maintainers,omitempty"`
URL string `json:"url,omitempty"`
NamespaceManifest string `json:"namespaceManifest,omitempty"`
}

// Maintainer describes an Operator maintainer.
Expand Down
1 change: 1 addition & 0 deletions pkg/kudoctl/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func newInstallCmd(fs afero.Fs) *cobra.Command {
installCmd.Flags().BoolVar(&options.SkipInstance, "skip-instance", false, "If set, install will install the Operator and OperatorVersion, but not an Instance. (default \"false\")")
installCmd.Flags().BoolVar(&options.Wait, "wait", false, "Specify if the CLI should wait for the install to complete before returning (default \"false\")")
installCmd.Flags().Int64Var(&options.WaitTime, "wait-time", 300, "Specify the max wait time in seconds for CLI for the install to complete before returning (default \"300\")")
installCmd.Flags().BoolVar(&options.CreateNameSpace, "create-namespace", false, "If set, install will create the specified namespace and will fail if it exists. (default \"false\")")

return installCmd
}
3 changes: 2 additions & 1 deletion pkg/kudoctl/cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Options struct {
RequestTimeout int64
Wait bool
WaitTime int64
CreateNameSpace bool
}

// DefaultOptions initializes the install command options to its defaults
Expand Down Expand Up @@ -78,5 +79,5 @@ func installOperator(operatorArgument string, options *Options, fs afero.Fs, set
return fmt.Errorf("failed to resolve operator package for: %s %w", operatorArgument, err)
}

return kudo.InstallPackage(kc, pkg.Resources, options.SkipInstance, options.InstanceName, settings.Namespace, options.Parameters, options.Wait, time.Duration(options.WaitTime)*time.Second)
return kudo.InstallPackage(kc, pkg.Resources, options.SkipInstance, options.InstanceName, settings.Namespace, options.Parameters, options.Wait, options.CreateNameSpace, time.Duration(options.WaitTime)*time.Second)
}
2 changes: 2 additions & 0 deletions pkg/kudoctl/cmd/testdata/deploy-kudo-ns.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spec:
type: string
type: object
type: array
namespaceManifest:
type: string
url:
type: string
type: object
Expand Down
2 changes: 2 additions & 0 deletions pkg/kudoctl/cmd/testdata/deploy-kudo-sa.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spec:
type: string
type: object
type: array
namespaceManifest:
type: string
url:
type: string
type: object
Expand Down
2 changes: 2 additions & 0 deletions pkg/kudoctl/cmd/testdata/deploy-kudo-webhook.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spec:
type: string
type: object
type: array
namespaceManifest:
type: string
url:
type: string
type: object
Expand Down
2 changes: 2 additions & 0 deletions pkg/kudoctl/cmd/testdata/deploy-kudo.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spec:
type: string
type: object
type: array
namespaceManifest:
type: string
url:
type: string
type: object
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/kudoinit/crd/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/kudoctl/packages/convert/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func FilesToResources(files *packages.Files) (*packages.Resources, error) {
KubernetesVersion: files.Operator.KubernetesVersion,
Maintainers: files.Operator.Maintainers,
URL: files.Operator.URL,
NamespaceManifest: files.Operator.NamespaceManifest,
},
Status: v1beta1.OperatorStatus{},
}
Expand Down
1 change: 1 addition & 0 deletions pkg/kudoctl/packages/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ type OperatorFile struct {
URL string `json:"url,omitempty"`
Tasks []v1beta1.Task `json:"tasks"`
Plans map[string]v1beta1.Plan `json:"plans"`
NamespaceManifest string `json:"namespaceManifest,omitempty"`
}
16 changes: 15 additions & 1 deletion pkg/kudoctl/util/kudo/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ import (

// InstallPackage installs package resources.
// If skipInstance is set to true, only a package's Operator and OperatorVersion is installed.
func InstallPackage(kc *Client, resources *packages.Resources, skipInstance bool, instanceName, namespace string, parameters map[string]string, w bool, waitTime time.Duration) error {
func InstallPackage(kc *Client, resources *packages.Resources, skipInstance bool, instanceName, namespace string, parameters map[string]string, w bool, createNS bool, waitTime time.Duration) error {
// PRE-INSTALLATION SETUP
operatorName := resources.Operator.ObjectMeta.Name
clog.V(3).Printf("operator name: %v", operatorName)
operatorVersion := resources.OperatorVersion.Spec.Version
clog.V(3).Printf("operator version: %v", operatorVersion)

if createNS {
clog.V(3).Printf("creating namespace: %q", namespace)
var manifest string = ""
if resources.Operator.Spec.NamespaceManifest != "" {
clog.V(3).Printf("creating namespace with manifest named: %q", resources.Operator.Spec.NamespaceManifest)
manifest = resources.OperatorVersion.Spec.Templates[resources.Operator.Spec.NamespaceManifest]
}
err := kc.CreateNamespace(namespace, manifest)
if err != nil {
// failure to create namespace ends installation process
return err
}
}

// make sure that our instance object is up to date with overrides from commandline
applyInstanceOverrides(resources.Instance, instanceName, parameters)
// this validation cannot be done earlier because we need to do it after applying things from commandline
Expand Down
2 changes: 1 addition & 1 deletion pkg/kudoctl/util/kudo/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func Test_InstallPackage(t *testing.T) {
testResources.OperatorVersion.Spec.Parameters = tt.parameters
namespace := "default" //nolint:goconst

err := InstallPackage(kc, &testResources, tt.skipInstance, "", namespace, tt.installParameters, false, 0)
err := InstallPackage(kc, &testResources, tt.skipInstance, "", namespace, tt.installParameters, false, false, 0)
if tt.err != "" {
assert.ErrorContains(t, err, tt.err)
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"

// Import Kubernetes authentication providers to support GKE, etc.
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -394,6 +395,22 @@ func (c *Client) ValidateServerForOperator(operator *v1beta1.Operator) error {
return nil
}

func (c *Client) CreateNamespace(namespace, manifest string) error {

ns := &v1core.Namespace{}
if manifest != "" {
if err := yaml.Unmarshal([]byte(manifest), ns); err != nil {
return fmt.Errorf("unmarshalling namespace manifest file: %w", err)
}
}
ns.TypeMeta.Kind = "Namespace"
ns.Name = namespace
ns.Annotations["created-by"] = "kudo-cli"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this annotation needed for? And if it's actually needed, let's put it into labels.go with other annotations and prefix it with kudo.dev/???


_, err := c.kubeClientset.CoreV1().Namespaces().Create(ns)
return err
}

// getKubeVersion returns stringified version of k8s server
func getKubeVersion(client discovery.ServerVersionInterface) (string, error) {
v, err := client.ServerVersion()
Expand Down