From 168e2ba7ccd949cfd3cb3dd0d45ec609cae3a257 Mon Sep 17 00:00:00 2001 From: lamai93 Date: Tue, 4 Dec 2018 09:49:27 +0100 Subject: [PATCH] Modified Spec definition for license key. --- .../deployment/v1alpha/deployment_spec.go | 27 ++------ pkg/apis/deployment/v1alpha/license_spec.go | 63 +++++++++++++++++++ .../v1alpha/zz_generated.deepcopy.go | 27 ++++++-- pkg/deployment/images.go | 5 +- pkg/deployment/resources/license.go | 6 +- pkg/deployment/resources/pod_creator.go | 8 +-- scripts/kube_create_license_key_secret.sh | 8 +-- 7 files changed, 102 insertions(+), 42 deletions(-) create mode 100644 pkg/apis/deployment/v1alpha/license_spec.go diff --git a/pkg/apis/deployment/v1alpha/deployment_spec.go b/pkg/apis/deployment/v1alpha/deployment_spec.go index af0e8b194..d36c8b568 100644 --- a/pkg/apis/deployment/v1alpha/deployment_spec.go +++ b/pkg/apis/deployment/v1alpha/deployment_spec.go @@ -26,7 +26,6 @@ import ( "reflect" "github.com/arangodb/kube-arangodb/pkg/util" - "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" "github.com/pkg/errors" "k8s.io/api/core/v1" ) @@ -55,13 +54,13 @@ type DeploymentSpec struct { ImagePullPolicy *v1.PullPolicy `json:"imagePullPolicy,omitempty"` DowntimeAllowed *bool `json:"downtimeAllowed,omitempty"` DisableIPv6 *bool `json:"disableIPv6,omitempty"` - LicenseKey *string `json:"licenseKey,omitempty"` ExternalAccess ExternalAccessSpec `json:"externalAccess"` RocksDB RocksDBSpec `json:"rocksdb"` Authentication AuthenticationSpec `json:"auth"` TLS TLSSpec `json:"tls"` Sync SyncSpec `json:"sync"` + License LicenseSpec `json:"license"` Single ServerGroupSpec `json:"single"` Agents ServerGroupSpec `json:"agents"` @@ -126,20 +125,6 @@ func (s DeploymentSpec) IsAuthenticated() bool { return s.Authentication.IsAuthenticated() } -// HasLicenseKey returns true if a license key secret name was set -func (s DeploymentSpec) HasLicenseKey() bool { - return s.LicenseKey != nil -} - -// GetLicenseKey returns the license key if set. Empty string otherwise. -func (s DeploymentSpec) GetLicenseKey() string { - if s.HasLicenseKey() { - return *s.LicenseKey - } - - return "" -} - // IsSecure returns true when SSL is enabled func (s DeploymentSpec) IsSecure() bool { return s.TLS.IsSecure() @@ -220,9 +205,7 @@ func (s *DeploymentSpec) SetDefaultsFrom(source DeploymentSpec) { if s.DisableIPv6 == nil { s.DisableIPv6 = util.NewBoolOrNil(source.DisableIPv6) } - if s.LicenseKey == nil { - s.LicenseKey = util.NewStringOrNil(source.LicenseKey) - } + s.License.SetDefaultsFrom(source.License) s.ExternalAccess.SetDefaultsFrom(source.ExternalAccess) s.RocksDB.SetDefaultsFrom(source.RocksDB) s.Authentication.SetDefaultsFrom(source.Authentication) @@ -291,10 +274,8 @@ func (s *DeploymentSpec) Validate() error { if err := s.Chaos.Validate(); err != nil { return maskAny(errors.Wrap(err, "spec.chaos")) } - if s.HasLicenseKey() { - if err := k8sutil.ValidateResourceName(s.GetLicenseKey()); err != nil { - return maskAny(errors.Wrap(err, "spec.licenseKey")) - } + if err := s.License.Validate(); err != nil { + return maskAny(errors.Wrap(err, "spec.licenseKey")) } return nil } diff --git a/pkg/apis/deployment/v1alpha/license_spec.go b/pkg/apis/deployment/v1alpha/license_spec.go new file mode 100644 index 000000000..84c2586eb --- /dev/null +++ b/pkg/apis/deployment/v1alpha/license_spec.go @@ -0,0 +1,63 @@ +// +// DISCLAIMER +// +// Copyright 2018 ArangoDB GmbH, Cologne, Germany +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Copyright holder is ArangoDB GmbH, Cologne, Germany +// + +package v1alpha + +import ( + "github.com/arangodb/kube-arangodb/pkg/util" + "github.com/arangodb/kube-arangodb/pkg/util/k8sutil" +) + +// LicenseSpec holds the license related information +type LicenseSpec struct { + SecretName *string `json:"secretName,omitempty"` +} + +// HasSecretName returns true if a license key secret name was set +func (s LicenseSpec) HasSecretName() bool { + return s.SecretName != nil +} + +// GetSecretName returns the license key if set. Empty string otherwise. +func (s LicenseSpec) GetSecretName() string { + if s.HasSecretName() { + return *s.SecretName + } + + return "" +} + +// Validate validates the LicenseSpec +func (s LicenseSpec) Validate() error { + if s.HasSecretName() { + if err := k8sutil.ValidateResourceName(s.GetSecretName()); err != nil { + return err + } + } + + return nil +} + +// SetDefaultsFrom fills all values not set in s with values from other +func (s LicenseSpec) SetDefaultsFrom(other LicenseSpec) { + if !s.HasSecretName() { + s.SecretName = util.NewStringOrNil(other.SecretName) + } +} diff --git a/pkg/apis/deployment/v1alpha/zz_generated.deepcopy.go b/pkg/apis/deployment/v1alpha/zz_generated.deepcopy.go index 4dcfc02dd..009f2e2e4 100644 --- a/pkg/apis/deployment/v1alpha/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v1alpha/zz_generated.deepcopy.go @@ -244,16 +244,12 @@ func (in *DeploymentSpec) DeepCopyInto(out *DeploymentSpec) { *out = new(bool) **out = **in } - if in.LicenseKey != nil { - in, out := &in.LicenseKey, &out.LicenseKey - *out = new(string) - **out = **in - } in.ExternalAccess.DeepCopyInto(&out.ExternalAccess) in.RocksDB.DeepCopyInto(&out.RocksDB) in.Authentication.DeepCopyInto(&out.Authentication) in.TLS.DeepCopyInto(&out.TLS) in.Sync.DeepCopyInto(&out.Sync) + in.License.DeepCopyInto(&out.License) in.Single.DeepCopyInto(&out.Single) in.Agents.DeepCopyInto(&out.Agents) in.DBServers.DeepCopyInto(&out.DBServers) @@ -450,6 +446,27 @@ func (in ImageInfoList) DeepCopy() ImageInfoList { return *out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *LicenseSpec) DeepCopyInto(out *LicenseSpec) { + *out = *in + if in.SecretName != nil { + in, out := &in.SecretName, &out.SecretName + *out = new(string) + **out = **in + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new LicenseSpec. +func (in *LicenseSpec) DeepCopy() *LicenseSpec { + if in == nil { + return nil + } + out := new(LicenseSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *MemberStatus) DeepCopyInto(out *MemberStatus) { *out = *in diff --git a/pkg/deployment/images.go b/pkg/deployment/images.go index 6c36298d6..0295c04e6 100644 --- a/pkg/deployment/images.go +++ b/pkg/deployment/images.go @@ -114,7 +114,6 @@ func (ib *imagesBuilder) fetchArangoDBImageIDAndVersion(ctx context.Context, ima // Pod found if k8sutil.IsPodFailed(pod) { // Wait some time before deleting the pod - log.Debug().Msgf("now: %v, then: %v", time.Now(), pod.GetCreationTimestamp()) if time.Now().After(pod.GetCreationTimestamp().Add(30 * time.Second)) { if err := ib.KubeCli.CoreV1().Pods(ns).Delete(podName, nil); err != nil && !k8sutil.IsNotFound(err) { log.Warn().Err(err).Msg("Failed to delete Image ID Pod") @@ -192,9 +191,9 @@ func (ib *imagesBuilder) fetchArangoDBImageIDAndVersion(ctx context.Context, ima serviceAccountName := "" env := make(map[string]k8sutil.EnvValue) - if ib.Spec.HasLicenseKey() { + if ib.Spec.License.HasSecretName() { env[constants.EnvArangoLicenseKey] = k8sutil.EnvValue{ - SecretName: ib.Spec.GetLicenseKey(), + SecretName: ib.Spec.License.GetSecretName(), SecretKey: constants.SecretKeyToken, } } diff --git a/pkg/deployment/resources/license.go b/pkg/deployment/resources/license.go index b2a784258..be1281484 100644 --- a/pkg/deployment/resources/license.go +++ b/pkg/deployment/resources/license.go @@ -29,10 +29,10 @@ import ( // ValidateLicenseKeySecret checks if the licens key secret exists and is valid func (r *Resources) ValidateLicenseKeySecret() error { - spec := r.context.GetSpec() + spec := r.context.GetSpec().License - if spec.HasLicenseKey() { - secretName := spec.GetLicenseKey() + if spec.HasSecretName() { + secretName := spec.GetSecretName() kubecli := r.context.GetKubeCli() ns := r.context.GetNamespace() diff --git a/pkg/deployment/resources/pod_creator.go b/pkg/deployment/resources/pod_creator.go index a332b7163..1264423b8 100644 --- a/pkg/deployment/resources/pod_creator.go +++ b/pkg/deployment/resources/pod_creator.go @@ -526,9 +526,9 @@ func (r *Resources) createPodForMember(spec api.DeploymentSpec, memberID string, } } - if spec.HasLicenseKey() { + if spec.License.HasSecretName() { env[constants.EnvArangoLicenseKey] = k8sutil.EnvValue{ - SecretName: spec.GetLicenseKey(), + SecretName: spec.License.GetSecretName(), SecretKey: constants.SecretKeyToken, } } @@ -599,9 +599,9 @@ func (r *Resources) createPodForMember(spec api.DeploymentSpec, memberID string, SecretKey: constants.SecretKeyToken, } } - if spec.HasLicenseKey() { + if spec.License.HasSecretName() { env[constants.EnvArangoLicenseKey] = k8sutil.EnvValue{ - SecretName: spec.GetLicenseKey(), + SecretName: spec.License.GetSecretName(), SecretKey: constants.SecretKeyToken, } } diff --git a/scripts/kube_create_license_key_secret.sh b/scripts/kube_create_license_key_secret.sh index a76a78eed..372e17254 100755 --- a/scripts/kube_create_license_key_secret.sh +++ b/scripts/kube_create_license_key_secret.sh @@ -1,13 +1,13 @@ #!/bin/sh -if [ -z $2 ]; then +LICENSE=$2 +NS=$1 + +if [ -z $LICENSE ]; then echo "No enterprise license set" exit 0 fi -LICENSE=$(echo "${2}" | base64 -w 0 ) -NS=$1 - if [ -z $NS ]; then echo "Specify a namespace argument" exit 1