diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ffbebc3d..24ac74fda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) - Allow to mount EmptyDir +- Allow to specify initContainers in pods ## [1.1.0](https://github.com/arangodb/kube-arangodb/tree/master) (2020-10-14) - Change NumberOfCores and MemoryOverride flags to be set to true by default diff --git a/pkg/apis/deployment/v1/deployment.go b/pkg/apis/deployment/v1/deployment.go index 39bffa8dc..ba72b346f 100644 --- a/pkg/apis/deployment/v1/deployment.go +++ b/pkg/apis/deployment/v1/deployment.go @@ -24,18 +24,18 @@ package v1 import ( "github.com/arangodb/kube-arangodb/pkg/apis/deployment" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + meta "k8s.io/apimachinery/pkg/apis/meta/v1" ) // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ArangoDeploymentList is a list of ArangoDB clusters. type ArangoDeploymentList struct { - metav1.TypeMeta `json:",inline"` + meta.TypeMeta `json:",inline"` // Standard list metadata // More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#metadata - metav1.ListMeta `json:"metadata,omitempty"` - Items []ArangoDeployment `json:"items"` + meta.ListMeta `json:"metadata,omitempty"` + Items []ArangoDeployment `json:"items"` } // +genclient @@ -43,18 +43,18 @@ type ArangoDeploymentList struct { // ArangoDeployment contains the entire Kubernetes info for an ArangoDB database deployment. type ArangoDeployment struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - Spec DeploymentSpec `json:"spec,omitempty"` - Status DeploymentStatus `json:"status,omitempty"` + meta.TypeMeta `json:",inline"` + meta.ObjectMeta `json:"metadata,omitempty"` + Spec DeploymentSpec `json:"spec,omitempty"` + Status DeploymentStatus `json:"status,omitempty"` } type ServerGroupFunc func(ServerGroup, ServerGroupSpec, *MemberStatusList) error // AsOwner creates an OwnerReference for the given deployment -func (d *ArangoDeployment) AsOwner() metav1.OwnerReference { +func (d *ArangoDeployment) AsOwner() meta.OwnerReference { trueVar := true - return metav1.OwnerReference{ + return meta.OwnerReference{ APIVersion: SchemeGroupVersion.String(), Kind: deployment.ArangoDeploymentResourceKind, Name: d.Name, diff --git a/pkg/apis/deployment/v1/deployment_spec.go b/pkg/apis/deployment/v1/deployment_spec.go index 25a74c7fd..04eaf926f 100644 --- a/pkg/apis/deployment/v1/deployment_spec.go +++ b/pkg/apis/deployment/v1/deployment_spec.go @@ -30,7 +30,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util" "github.com/pkg/errors" - v1 "k8s.io/api/core/v1" + core "k8s.io/api/core/v1" ) var ( @@ -39,9 +39,9 @@ var ( // validatePullPolicy the image pull policy. // Return errors when validation fails, nil on success. -func validatePullPolicy(v v1.PullPolicy) error { +func validatePullPolicy(v core.PullPolicy) error { switch v { - case "", v1.PullAlways, v1.PullNever, v1.PullIfNotPresent: + case "", core.PullAlways, core.PullNever, core.PullIfNotPresent: return nil default: return maskAny(errors.Wrapf(ValidationError, "Unknown pull policy: '%s'", string(v))) @@ -54,7 +54,7 @@ type DeploymentSpec struct { Environment *Environment `json:"environment,omitempty"` StorageEngine *StorageEngine `json:"storageEngine,omitempty"` Image *string `json:"image,omitempty"` - ImagePullPolicy *v1.PullPolicy `json:"imagePullPolicy,omitempty"` + ImagePullPolicy *core.PullPolicy `json:"imagePullPolicy,omitempty"` ImagePullSecrets []string `json:"imagePullSecrets,omitempty"` ImageDiscoveryMode *DeploymentImageDiscoveryModeSpec `json:"imageDiscoveryMode,omitempty"` DowntimeAllowed *bool `json:"downtimeAllowed,omitempty"` @@ -161,7 +161,7 @@ func (s DeploymentSpec) GetSyncImage() string { } // GetImagePullPolicy returns the value of imagePullPolicy. -func (s DeploymentSpec) GetImagePullPolicy() v1.PullPolicy { +func (s DeploymentSpec) GetImagePullPolicy() core.PullPolicy { return util.PullPolicyOrDefault(s.ImagePullPolicy) } @@ -253,7 +253,7 @@ func (s *DeploymentSpec) SetDefaults(deploymentName string) { s.Image = util.NewString(DefaultImage) } if s.GetImagePullPolicy() == "" { - s.ImagePullPolicy = util.NewPullPolicy(v1.PullIfNotPresent) + s.ImagePullPolicy = util.NewPullPolicy(core.PullIfNotPresent) } s.ExternalAccess.SetDefaults() s.RocksDB.SetDefaults() diff --git a/pkg/apis/deployment/v1/server_group.go b/pkg/apis/deployment/v1/server_group.go index 727fb470a..2d89d4293 100644 --- a/pkg/apis/deployment/v1/server_group.go +++ b/pkg/apis/deployment/v1/server_group.go @@ -27,27 +27,30 @@ import "time" type ServerGroup int const ( - ServerGroupUnknown ServerGroup = 0 - ServerGroupSingle ServerGroup = 1 - ServerGroupAgents ServerGroup = 2 - ServerGroupDBServers ServerGroup = 3 - ServerGroupCoordinators ServerGroup = 4 - ServerGroupSyncMasters ServerGroup = 5 - ServerGroupSyncWorkers ServerGroup = 6 - - ServerGroupSingleString = "single" - ServerGroupAgentsString = "agent" - ServerGroupDBServersString = "dbserver" - ServerGroupCoordinatorsString = "coordinator" - ServerGroupSyncMastersString = "syncmaster" - ServerGroupSyncWorkersString = "syncworker" - - ServerGroupSingleAbbreviatedString = "sngl" - ServerGroupAgentsAbbreviatedString = "agnt" - ServerGroupDBServersAbbreviatedString = "prmr" - ServerGroupCoordinatorsAbbreviatedString = "crdn" - ServerGroupSyncMastersAbbreviatedString = "syma" - ServerGroupSyncWorkersAbbreviatedString = "sywo" + ServerGroupUnknown ServerGroup = 0 + ServerGroupSingle ServerGroup = 1 + ServerGroupAgents ServerGroup = 2 + ServerGroupDBServers ServerGroup = 3 + ServerGroupCoordinators ServerGroup = 4 + ServerGroupSyncMasters ServerGroup = 5 + ServerGroupSyncWorkers ServerGroup = 6 + ServerGroupImageDiscovery ServerGroup = -1 + + ServerGroupSingleString = "single" + ServerGroupAgentsString = "agent" + ServerGroupDBServersString = "dbserver" + ServerGroupCoordinatorsString = "coordinator" + ServerGroupSyncMastersString = "syncmaster" + ServerGroupSyncWorkersString = "syncworker" + ServerGroupImageDiscoveryString = "imagediscovery" + + ServerGroupSingleAbbreviatedString = "sngl" + ServerGroupAgentsAbbreviatedString = "agnt" + ServerGroupDBServersAbbreviatedString = "prmr" + ServerGroupCoordinatorsAbbreviatedString = "crdn" + ServerGroupSyncMastersAbbreviatedString = "syma" + ServerGroupSyncWorkersAbbreviatedString = "sywo" + ServerGroupImageDiscoveryAbbreviatedString = "id" ) var ( diff --git a/pkg/apis/deployment/v1/server_group_init_containers.go b/pkg/apis/deployment/v1/server_group_init_containers.go new file mode 100644 index 000000000..81397fd33 --- /dev/null +++ b/pkg/apis/deployment/v1/server_group_init_containers.go @@ -0,0 +1,127 @@ +// +// DISCLAIMER +// +// Copyright 2020 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 +// +// Author Adam Janikowski +// + +package v1 + +import ( + "github.com/arangodb/kube-arangodb/pkg/apis/shared" + sharedv1 "github.com/arangodb/kube-arangodb/pkg/apis/shared/v1" + "github.com/pkg/errors" + core "k8s.io/api/core/v1" +) + +const ( + ServerGroupReservedInitContainerNameLifecycle = "init-lifecycle" + ServerGroupReservedInitContainerNameUUID = "uuid" +) + +func IsReservedServerGroupInitContainerName(name string) bool { + switch name { + case ServerGroupReservedInitContainerNameLifecycle, ServerGroupReservedInitContainerNameUUID: + return true + default: + return false + } +} + +func ValidateServerGroupInitContainerName(name string) error { + if IsReservedServerGroupInitContainerName(name) { + return errors.Errorf("InitContainer name %s is restricted", name) + } + + return sharedv1.AsKubernetesResourceName(&name).Validate() +} + +type ServerGroupInitContainerMode string + +func (s *ServerGroupInitContainerMode) Get() ServerGroupInitContainerMode { + if s == nil { + return ServerGroupInitContainerUpdateMode // default + } + + return *s +} + +func (s ServerGroupInitContainerMode) New() *ServerGroupInitContainerMode { + return &s +} + +func (s *ServerGroupInitContainerMode) Validate() error { + switch v := s.Get(); v { + case ServerGroupInitContainerIgnoreMode, ServerGroupInitContainerUpdateMode: + return nil + default: + return errors.Errorf("Unknown serverGroupInitContainerMode %s", v) + } +} + +const ( + // ServerGroupInitContainerIgnoreMode ignores init container changes in pod recreation flow + ServerGroupInitContainerIgnoreMode ServerGroupInitContainerMode = "ignore" + // ServerGroupInitContainerUpdateMode enforce update of pod if init container has been changed + ServerGroupInitContainerUpdateMode ServerGroupInitContainerMode = "update" +) + +type ServerGroupInitContainers struct { + // Containers contains list of containers + Containers []core.Container `json:"containers,omitempty"` + + // Mode keep container replace mode + Mode *ServerGroupInitContainerMode `json:"mode,omitempty"` +} + +func (s *ServerGroupInitContainers) GetMode() *ServerGroupInitContainerMode { + if s == nil { + return nil + } + + return s.Mode +} + +func (s *ServerGroupInitContainers) GetContainers() []core.Container { + if s == nil { + return nil + } + + return s.Containers +} + +func (s *ServerGroupInitContainers) Validate() error { + if s == nil { + return nil + } + + return shared.WithErrors( + shared.PrefixResourceError("mode", s.Mode.Validate()), + shared.PrefixResourceError("containers", s.validateInitContainers()), + ) +} + +func (s *ServerGroupInitContainers) validateInitContainers() error { + for _, c := range s.Containers { + if err := ValidateServerGroupInitContainerName(c.Name); err != nil { + return err + } + } + + return nil +} diff --git a/pkg/apis/deployment/v1/server_group_spec.go b/pkg/apis/deployment/v1/server_group_spec.go index 5dd35b5dc..943e0383d 100644 --- a/pkg/apis/deployment/v1/server_group_spec.go +++ b/pkg/apis/deployment/v1/server_group_spec.go @@ -100,6 +100,8 @@ type ServerGroupSpec struct { VolumeMounts ServerGroupSpecVolumeMounts `json:"volumeMounts,omitempty"` // ExtendedRotationCheck extend checks for rotation ExtendedRotationCheck *bool `json:"extendedRotationCheck,omitempty"` + // InitContainers Init containers specification + InitContainers *ServerGroupInitContainers `json:"initContainers,omitempty"` } // ServerGroupSpecSecurityContext contains specification for pod security context @@ -471,6 +473,7 @@ func (s *ServerGroupSpec) validate() error { return shared.WithErrors( shared.PrefixResourceError("volumes", s.Volumes.Validate()), shared.PrefixResourceError("volumeMounts", s.VolumeMounts.Validate()), + shared.PrefixResourceError("initContainers", s.InitContainers.Validate()), s.validateVolumes(), ) } @@ -482,12 +485,30 @@ func (s *ServerGroupSpec) validateVolumes() error { volumes[volume.Name] = true } + volumes["arangod-data"] = true + for _, mount := range s.VolumeMounts { if _, ok := volumes[mount.Name]; !ok { return errors.Errorf("Volume %s is not defined, but required by mount", mount.Name) } } + for _, container := range s.InitContainers.GetContainers() { + for _, mount := range container.VolumeMounts { + if _, ok := volumes[mount.Name]; !ok { + return errors.Errorf("Volume %s is not defined, but required by mount in init container %s", mount.Name, container.Name) + } + } + } + + for _, container := range s.Sidecars { + for _, mount := range s.VolumeMounts { + if _, ok := volumes[mount.Name]; !ok { + return errors.Errorf("Volume %s is not defined, but required by mount in sidecar %s", mount.Name, container.Name) + } + } + } + return nil } diff --git a/pkg/apis/deployment/v1/zz_generated.deepcopy.go b/pkg/apis/deployment/v1/zz_generated.deepcopy.go index bb586bba9..cc58a8687 100644 --- a/pkg/apis/deployment/v1/zz_generated.deepcopy.go +++ b/pkg/apis/deployment/v1/zz_generated.deepcopy.go @@ -1080,6 +1080,34 @@ func (in ServerGroupEnvVars) DeepCopy() ServerGroupEnvVars { return *out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ServerGroupInitContainers) DeepCopyInto(out *ServerGroupInitContainers) { + *out = *in + if in.Containers != nil { + in, out := &in.Containers, &out.Containers + *out = make([]corev1.Container, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Mode != nil { + in, out := &in.Mode, &out.Mode + *out = new(ServerGroupInitContainerMode) + **out = **in + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ServerGroupInitContainers. +func (in *ServerGroupInitContainers) DeepCopy() *ServerGroupInitContainers { + if in == nil { + return nil + } + out := new(ServerGroupInitContainers) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ServerGroupProbeSpec) DeepCopyInto(out *ServerGroupProbeSpec) { *out = *in @@ -1320,6 +1348,11 @@ func (in *ServerGroupSpec) DeepCopyInto(out *ServerGroupSpec) { *out = new(bool) **out = **in } + if in.InitContainers != nil { + in, out := &in.InitContainers, &out.InitContainers + *out = new(ServerGroupInitContainers) + (*in).DeepCopyInto(*out) + } return } diff --git a/pkg/deployment/images.go b/pkg/deployment/images.go index 5146b08d6..ac140e30f 100644 --- a/pkg/deployment/images.go +++ b/pkg/deployment/images.go @@ -218,7 +218,7 @@ func (ib *imagesBuilder) fetchArangoDBImageIDAndVersion(ctx context.Context, ima return true, maskAny(err) } - if _, _, err := resources.CreateArangoPod(ib.KubeCli, ib.APIObject, pod); err != nil { + if _, err := resources.CreateArangoPod(ib.KubeCli, ib.APIObject, ib.Spec, api.ServerGroupImageDiscovery, pod); err != nil { log.Debug().Err(err).Msg("Failed to create image ID pod") return true, maskAny(err) } diff --git a/pkg/deployment/reconcile/plan_builder_rotate_upgrade.go b/pkg/deployment/reconcile/plan_builder_rotate_upgrade.go index 22f63dfd0..39f99a8a5 100644 --- a/pkg/deployment/reconcile/plan_builder_rotate_upgrade.go +++ b/pkg/deployment/reconcile/plan_builder_rotate_upgrade.go @@ -25,6 +25,8 @@ package reconcile import ( "context" + "github.com/arangodb/kube-arangodb/pkg/deployment/resources" + "github.com/arangodb/go-driver" upgraderules "github.com/arangodb/go-upgrade-rules" "github.com/arangodb/kube-arangodb/pkg/apis/deployment" @@ -264,13 +266,15 @@ func podNeedsRotation(log zerolog.Logger, p *core.Pod, apiObject metav1.Object, imageInfo = *m.Image } + groupSpec := spec.GetServerGroupSpec(group) + renderedPod, err := context.RenderPodForMember(cachedStatus, spec, status, m.ID, imageInfo) if err != nil { log.Err(err).Msg("Error while rendering pod") return false, "" } - checksum, err := k8sutil.GetPodSpecChecksum(renderedPod.Spec) + checksum, err := resources.ChecksumArangoPod(groupSpec, renderedPod) if err != nil { log.Err(err).Msg("Error while getting pod checksum") return false, "" diff --git a/pkg/deployment/resources/pod_creator.go b/pkg/deployment/resources/pod_creator.go index 8e284b276..052dcc038 100644 --- a/pkg/deployment/resources/pod_creator.go +++ b/pkg/deployment/resources/pod_creator.go @@ -24,6 +24,7 @@ package resources import ( "crypto/sha1" + "crypto/sha256" "encoding/json" "fmt" "net" @@ -460,13 +461,18 @@ func (r *Resources) createPodForMember(spec api.DeploymentSpec, memberID string, newPhase = api.MemberPhaseUpgrading } - uid, checksum, err := CreateArangoPod(kubecli, apiObject, pod) + sha, err := ChecksumArangoPod(groupSpec, pod) + if err != nil { + return maskAny(err) + } + + uid, err := CreateArangoPod(kubecli, apiObject, spec, group, pod) if err != nil { return maskAny(err) } m.PodUID = uid - m.PodSpecVersion = checksum + m.PodSpecVersion = sha m.ArangoVersion = status.CurrentImage.ArangoDBVersion m.ImageID = status.CurrentImage.ImageID @@ -504,14 +510,19 @@ func (r *Resources) createPodForMember(spec api.DeploymentSpec, memberID string, } } - uid, checksum, err := CreateArangoPod(kubecli, apiObject, pod) + sha, err := ChecksumArangoPod(groupSpec, pod) + if err != nil { + return maskAny(err) + } + + uid, err := CreateArangoPod(kubecli, apiObject, spec, group, pod) if err != nil { return maskAny(err) } log.Debug().Str("pod-name", m.PodName).Msg("Created pod") m.PodUID = uid - m.PodSpecVersion = checksum + m.PodSpecVersion = sha } // Record new member phase m.Phase = newPhase @@ -589,12 +600,30 @@ func RenderArangoPod(deployment k8sutil.APIObject, role, id, podName string, // CreateArangoPod creates a new Pod with container provided by parameter 'containerCreator' // If the pod already exists, nil is returned. // If another error occurs, that error is returned. -func CreateArangoPod(kubecli kubernetes.Interface, deployment k8sutil.APIObject, pod *core.Pod) (types.UID, string, error) { - uid, checksum, err := k8sutil.CreatePod(kubecli, pod, deployment.GetNamespace(), deployment.AsOwner()) +func CreateArangoPod(kubecli kubernetes.Interface, deployment k8sutil.APIObject, deploymentSpec api.DeploymentSpec, group api.ServerGroup, pod *core.Pod) (types.UID, error) { + uid, err := k8sutil.CreatePod(kubecli, pod, deployment.GetNamespace(), deployment.AsOwner()) if err != nil { - return "", "", maskAny(err) + return "", maskAny(err) } - return uid, checksum, nil + + return uid, nil +} + +func ChecksumArangoPod(groupSpec api.ServerGroupSpec, pod *core.Pod) (string, error) { + shaPod := pod.DeepCopy() + switch groupSpec.InitContainers.GetMode().Get() { + case api.ServerGroupInitContainerUpdateMode: + shaPod.Spec.InitContainers = groupSpec.InitContainers.GetContainers() + default: + shaPod.Spec.InitContainers = nil + } + + data, err := json.Marshal(shaPod.Spec) + if err != nil { + return "", err + } + + return fmt.Sprintf("%0x", sha256.Sum256(data)), nil } // EnsurePods creates all Pods listed in member status diff --git a/pkg/deployment/resources/pod_creator_arangod.go b/pkg/deployment/resources/pod_creator_arangod.go index 8a64f8a9c..e06736e5a 100644 --- a/pkg/deployment/resources/pod_creator_arangod.go +++ b/pkg/deployment/resources/pod_creator_arangod.go @@ -395,6 +395,10 @@ func (m *MemberArangoDPod) IsDeploymentMode() bool { func (m *MemberArangoDPod) GetInitContainers() ([]core.Container, error) { var initContainers []core.Container + if c := m.groupSpec.InitContainers.GetContainers(); len(c) > 0 { + initContainers = append(initContainers, c...) + } + executable, err := os.Executable() if err != nil { return nil, err diff --git a/pkg/deployment/resources/pod_creator_sync.go b/pkg/deployment/resources/pod_creator_sync.go index ff97a2c21..ed4ded172 100644 --- a/pkg/deployment/resources/pod_creator_sync.go +++ b/pkg/deployment/resources/pod_creator_sync.go @@ -267,6 +267,10 @@ func (m *MemberSyncPod) IsDeploymentMode() bool { func (m *MemberSyncPod) GetInitContainers() ([]core.Container, error) { var initContainers []core.Container + if c := m.groupSpec.InitContainers.GetContainers(); len(c) > 0 { + initContainers = append(initContainers, c...) + } + lifecycleImage := m.resources.context.GetLifecycleImage() if lifecycleImage != "" { c, err := k8sutil.InitLifecycleContainer(lifecycleImage, &m.spec.Lifecycle.Resources, diff --git a/pkg/util/k8sutil/lifecycle.go b/pkg/util/k8sutil/lifecycle.go index 0856618c5..ef7586532 100644 --- a/pkg/util/k8sutil/lifecycle.go +++ b/pkg/util/k8sutil/lifecycle.go @@ -28,7 +28,7 @@ import ( "github.com/arangodb/kube-arangodb/pkg/util/constants" - v1 "k8s.io/api/core/v1" + core "k8s.io/api/core/v1" ) const ( @@ -38,19 +38,19 @@ const ( ) // InitLifecycleContainer creates an init-container to copy the lifecycle binary to a shared volume. -func InitLifecycleContainer(image string, resources *v1.ResourceRequirements, securityContext *v1.SecurityContext) (v1.Container, error) { +func InitLifecycleContainer(image string, resources *core.ResourceRequirements, securityContext *core.SecurityContext) (core.Container, error) { binaryPath, err := os.Executable() if err != nil { - return v1.Container{}, maskAny(err) + return core.Container{}, maskAny(err) } - c := v1.Container{ + c := core.Container{ Name: initLifecycleContainerName, Image: image, Command: append([]string{binaryPath}, "lifecycle", "copy", "--target", LifecycleVolumeMountDir), - VolumeMounts: []v1.VolumeMount{ + VolumeMounts: []core.VolumeMount{ LifecycleVolumeMount(), }, - ImagePullPolicy: v1.PullIfNotPresent, + ImagePullPolicy: core.PullIfNotPresent, SecurityContext: securityContext, } @@ -61,15 +61,15 @@ func InitLifecycleContainer(image string, resources *v1.ResourceRequirements, se } // NewLifecycle creates a lifecycle structure with preStop handler. -func NewLifecycle() (*v1.Lifecycle, error) { +func NewLifecycle() (*core.Lifecycle, error) { binaryPath, err := os.Executable() if err != nil { return nil, maskAny(err) } exePath := filepath.Join(LifecycleVolumeMountDir, filepath.Base(binaryPath)) - lifecycle := &v1.Lifecycle{ - PreStop: &v1.Handler{ - Exec: &v1.ExecAction{ + lifecycle := &core.Lifecycle{ + PreStop: &core.Handler{ + Exec: &core.ExecAction{ Command: append([]string{exePath}, "lifecycle", "preStop"), }, }, @@ -78,8 +78,8 @@ func NewLifecycle() (*v1.Lifecycle, error) { return lifecycle, nil } -func GetLifecycleEnv() []v1.EnvVar { - return []v1.EnvVar{ +func GetLifecycleEnv() []core.EnvVar { + return []core.EnvVar{ CreateEnvFieldPath(constants.EnvOperatorPodName, "metadata.name"), CreateEnvFieldPath(constants.EnvOperatorPodNamespace, "metadata.namespace"), CreateEnvFieldPath(constants.EnvOperatorNodeName, "spec.nodeName"), @@ -88,19 +88,19 @@ func GetLifecycleEnv() []v1.EnvVar { } // LifecycleVolumeMount creates a volume mount structure for shared lifecycle emptyDir. -func LifecycleVolumeMount() v1.VolumeMount { - return v1.VolumeMount{ +func LifecycleVolumeMount() core.VolumeMount { + return core.VolumeMount{ Name: lifecycleVolumeName, MountPath: LifecycleVolumeMountDir, } } // LifecycleVolume creates a volume mount structure for shared lifecycle emptyDir. -func LifecycleVolume() v1.Volume { - return v1.Volume{ +func LifecycleVolume() core.Volume { + return core.Volume{ Name: lifecycleVolumeName, - VolumeSource: v1.VolumeSource{ - EmptyDir: &v1.EmptyDirVolumeSource{}, + VolumeSource: core.VolumeSource{ + EmptyDir: &core.EmptyDirVolumeSource{}, }, } } diff --git a/pkg/util/k8sutil/pods.go b/pkg/util/k8sutil/pods.go index fc606300e..b3e591e92 100644 --- a/pkg/util/k8sutil/pods.go +++ b/pkg/util/k8sutil/pods.go @@ -373,11 +373,8 @@ func NewPod(deploymentName, role, id, podName string, podCreator interfaces.PodC return p } -// GetPodSpecChecksum return checksum of requested pod spec +// GetPodSpecChecksum return checksum of requested pod spec based on deployment and group spec func GetPodSpecChecksum(podSpec core.PodSpec) (string, error) { - // Do not calculate init containers - podSpec.InitContainers = nil - data, err := json.Marshal(podSpec) if err != nil { return "", err @@ -389,18 +386,13 @@ func GetPodSpecChecksum(podSpec core.PodSpec) (string, error) { // CreatePod adds an owner to the given pod and calls the k8s api-server to created it. // If the pod already exists, nil is returned. // If another error occurs, that error is returned. -func CreatePod(kubecli kubernetes.Interface, pod *core.Pod, ns string, owner metav1.OwnerReference) (types.UID, string, error) { +func CreatePod(kubecli kubernetes.Interface, pod *core.Pod, ns string, owner metav1.OwnerReference) (types.UID, error) { AddOwnerRefToObject(pod.GetObjectMeta(), &owner) - checksum, err := GetPodSpecChecksum(pod.Spec) - if err != nil { - return "", "", err - } - if pod, err := kubecli.CoreV1().Pods(ns).Create(pod); err != nil && !IsAlreadyExists(err) { - return "", "", maskAny(err) + return "", maskAny(err) } else { - return pod.UID, checksum, nil + return pod.UID, nil } }