Skip to content

Commit

Permalink
enhancement(CollaSet): add validation and defaulting for CollaSet
Browse files Browse the repository at this point in the history
  • Loading branch information
wu8685 committed Aug 28, 2023
1 parent 74d27c7 commit f82f2ac
Show file tree
Hide file tree
Showing 8 changed files with 1,077 additions and 2 deletions.
26 changes: 25 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ require (
k8s.io/apimachinery v0.22.6
k8s.io/client-go v0.22.6
k8s.io/component-base v0.22.6
k8s.io/component-helpers v0.0.0-00010101000000-000000000000
k8s.io/component-helpers v0.22.6
k8s.io/klog/v2 v2.80.1
k8s.io/kubernetes v0.0.0-00010101000000-000000000000
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/controller-runtime v0.10.3
)
Expand All @@ -34,6 +35,7 @@ require (
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cyphar/filepath-securejoin v0.2.2 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
Expand All @@ -51,6 +53,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
Expand All @@ -72,6 +75,7 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.22.2 // indirect
k8s.io/apiserver v0.22.6 // indirect
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
Expand All @@ -84,9 +88,29 @@ replace (
k8s.io/api => k8s.io/api v0.22.6
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.22.6
k8s.io/apimachinery => k8s.io/apimachinery v0.22.6
k8s.io/apiserver => k8s.io/apiserver v0.22.6
k8s.io/cli-runtime => k8s.io/cli-runtime v0.22.6
k8s.io/client-go => k8s.io/client-go v0.22.6
k8s.io/cloud-provider => k8s.io/cloud-provider v0.22.6
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.22.6
k8s.io/code-generator => k8s.io/code-generator v0.22.6
k8s.io/component-base => k8s.io/component-base v0.22.6
k8s.io/component-helpers => k8s.io/component-helpers v0.22.6
k8s.io/controller-manager => k8s.io/controller-manager v0.22.6
k8s.io/cri-api => k8s.io/cri-api v0.22.6
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.22.6
k8s.io/klog/v2 => k8s.io/klog/v2 v2.9.0
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.22.6
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.22.6
k8s.io/kube-proxy => k8s.io/kube-proxy v0.22.6
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.22.6
k8s.io/kubectl => k8s.io/kubectl v0.22.6
k8s.io/kubelet => k8s.io/kubelet v0.22.6
k8s.io/kubernetes => k8s.io/kubernetes v1.22.6
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.22.6
k8s.io/metrics => k8s.io/metrics v0.22.6
k8s.io/mount-utils => k8s.io/mount-utils v0.22.6
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.22.6
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.22.6
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.10.3
)
223 changes: 223 additions & 0 deletions go.sum

Large diffs are not rendered by default.

306 changes: 306 additions & 0 deletions pkg/webhook/server/generic/collaset/collaset_mutating_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
/*
Copyright 2023 The KusionStack Authors.
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.
*/

package collaset

import (
"context"
"encoding/json"
"net/http"

admissionv1 "k8s.io/api/admission/v1"
"k8s.io/klog/v2"
k8scorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

appsv1alpha1 "kusionstack.io/kafed/apis/apps/v1alpha1"
)

type MutatingHandler struct {
client.Client
*admission.Decoder
}

func NewMutatingHandler() *MutatingHandler {
return &MutatingHandler{}
}

func (h *MutatingHandler) Handle(ctx context.Context, req admission.Request) (resp admission.Response) {
if req.Operation != admissionv1.Update && req.Operation != admissionv1.Create {
return admission.Allowed("")
}
cls := &appsv1alpha1.CollaSet{}
if err := h.Decode(req, cls); err != nil {
klog.Errorf("decode CollaSet validating request failed, %v", err)
return admission.Errored(http.StatusBadRequest, err)
}
h.setDetaultCollaSet(cls)
marshalled, err := json.Marshal(cls)
if err != nil {
klog.Errorf("marshal RuleSet failed, %v", err)
return admission.Errored(http.StatusInternalServerError, err)
}

return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
}

func (h *MutatingHandler) setDetaultCollaSet(cls *appsv1alpha1.CollaSet) {
h.setDefaultPodSpec(cls)
h.setDefaultCollaSetUpdateStrategy(cls)
}

func (h *MutatingHandler) setDefaultPodSpec(in *appsv1alpha1.CollaSet) {
k8scorev1.SetDefaults_PodSpec(&in.Spec.Template.Spec)
for i := range in.Spec.Template.Spec.Volumes {
a := &in.Spec.Template.Spec.Volumes[i]
k8scorev1.SetDefaults_Volume(a)
if a.VolumeSource.HostPath != nil {
k8scorev1.SetDefaults_HostPathVolumeSource(a.VolumeSource.HostPath)
}
if a.VolumeSource.Secret != nil {
k8scorev1.SetDefaults_SecretVolumeSource(a.VolumeSource.Secret)
}
if a.VolumeSource.ISCSI != nil {
k8scorev1.SetDefaults_ISCSIVolumeSource(a.VolumeSource.ISCSI)
}
if a.VolumeSource.RBD != nil {
k8scorev1.SetDefaults_RBDVolumeSource(a.VolumeSource.RBD)
}
if a.VolumeSource.DownwardAPI != nil {
k8scorev1.SetDefaults_DownwardAPIVolumeSource(a.VolumeSource.DownwardAPI)
for j := range a.VolumeSource.DownwardAPI.Items {
b := &a.VolumeSource.DownwardAPI.Items[j]
if b.FieldRef != nil {
k8scorev1.SetDefaults_ObjectFieldSelector(b.FieldRef)
}
}
}
if a.VolumeSource.ConfigMap != nil {
k8scorev1.SetDefaults_ConfigMapVolumeSource(a.VolumeSource.ConfigMap)
}
if a.VolumeSource.AzureDisk != nil {
k8scorev1.SetDefaults_AzureDiskVolumeSource(a.VolumeSource.AzureDisk)
}
if a.VolumeSource.Projected != nil {
k8scorev1.SetDefaults_ProjectedVolumeSource(a.VolumeSource.Projected)
for j := range a.VolumeSource.Projected.Sources {
b := &a.VolumeSource.Projected.Sources[j]
if b.DownwardAPI != nil {
for k := range b.DownwardAPI.Items {
c := &b.DownwardAPI.Items[k]
if c.FieldRef != nil {
k8scorev1.SetDefaults_ObjectFieldSelector(c.FieldRef)
}
}
}
if b.ServiceAccountToken != nil {
k8scorev1.SetDefaults_ServiceAccountTokenProjection(b.ServiceAccountToken)
}
}
}
if a.VolumeSource.ScaleIO != nil {
k8scorev1.SetDefaults_ScaleIOVolumeSource(a.VolumeSource.ScaleIO)
}
if a.VolumeSource.Ephemeral != nil {
if a.VolumeSource.Ephemeral.VolumeClaimTemplate != nil {
k8scorev1.SetDefaults_PersistentVolumeClaimSpec(&a.VolumeSource.Ephemeral.VolumeClaimTemplate.Spec)
k8scorev1.SetDefaults_ResourceList(&a.VolumeSource.Ephemeral.VolumeClaimTemplate.Spec.Resources.Limits)
k8scorev1.SetDefaults_ResourceList(&a.VolumeSource.Ephemeral.VolumeClaimTemplate.Spec.Resources.Requests)
}
}
}
for i := range in.Spec.Template.Spec.InitContainers {
a := &in.Spec.Template.Spec.InitContainers[i]
k8scorev1.SetDefaults_Container(a)
for j := range a.Ports {
b := &a.Ports[j]
if b.Protocol == "" {
b.Protocol = "TCP"
}
}
for j := range a.Env {
b := &a.Env[j]
if b.ValueFrom != nil {
if b.ValueFrom.FieldRef != nil {
k8scorev1.SetDefaults_ObjectFieldSelector(b.ValueFrom.FieldRef)
}
}
}
k8scorev1.SetDefaults_ResourceList(&a.Resources.Limits)
k8scorev1.SetDefaults_ResourceList(&a.Resources.Requests)
if a.LivenessProbe != nil {
k8scorev1.SetDefaults_Probe(a.LivenessProbe)
if a.LivenessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.LivenessProbe.Handler.HTTPGet)
}
}
if a.ReadinessProbe != nil {
k8scorev1.SetDefaults_Probe(a.ReadinessProbe)
if a.ReadinessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.ReadinessProbe.Handler.HTTPGet)
}
}
if a.StartupProbe != nil {
k8scorev1.SetDefaults_Probe(a.StartupProbe)
if a.StartupProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.StartupProbe.Handler.HTTPGet)
}
}
if a.Lifecycle != nil {
if a.Lifecycle.PostStart != nil {
if a.Lifecycle.PostStart.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.Lifecycle.PostStart.HTTPGet)
}
}
if a.Lifecycle.PreStop != nil {
if a.Lifecycle.PreStop.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.Lifecycle.PreStop.HTTPGet)
}
}
}
}
for i := range in.Spec.Template.Spec.Containers {
a := &in.Spec.Template.Spec.Containers[i]
k8scorev1.SetDefaults_Container(a)
for j := range a.Ports {
b := &a.Ports[j]
if b.Protocol == "" {
b.Protocol = "TCP"
}
}
for j := range a.Env {
b := &a.Env[j]
if b.ValueFrom != nil {
if b.ValueFrom.FieldRef != nil {
k8scorev1.SetDefaults_ObjectFieldSelector(b.ValueFrom.FieldRef)
}
}
}
k8scorev1.SetDefaults_ResourceList(&a.Resources.Limits)
k8scorev1.SetDefaults_ResourceList(&a.Resources.Requests)
if a.LivenessProbe != nil {
k8scorev1.SetDefaults_Probe(a.LivenessProbe)
if a.LivenessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.LivenessProbe.Handler.HTTPGet)
}
}
if a.ReadinessProbe != nil {
k8scorev1.SetDefaults_Probe(a.ReadinessProbe)
if a.ReadinessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.ReadinessProbe.Handler.HTTPGet)
}
}
if a.StartupProbe != nil {
k8scorev1.SetDefaults_Probe(a.StartupProbe)
if a.StartupProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.StartupProbe.Handler.HTTPGet)
}
}
if a.Lifecycle != nil {
if a.Lifecycle.PostStart != nil {
if a.Lifecycle.PostStart.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.Lifecycle.PostStart.HTTPGet)
}
}
if a.Lifecycle.PreStop != nil {
if a.Lifecycle.PreStop.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.Lifecycle.PreStop.HTTPGet)
}
}
}
}
for i := range in.Spec.Template.Spec.EphemeralContainers {
a := &in.Spec.Template.Spec.EphemeralContainers[i]
k8scorev1.SetDefaults_EphemeralContainer(a)
for j := range a.EphemeralContainerCommon.Ports {
b := &a.EphemeralContainerCommon.Ports[j]
if b.Protocol == "" {
b.Protocol = "TCP"
}
}
for j := range a.EphemeralContainerCommon.Env {
b := &a.EphemeralContainerCommon.Env[j]
if b.ValueFrom != nil {
if b.ValueFrom.FieldRef != nil {
k8scorev1.SetDefaults_ObjectFieldSelector(b.ValueFrom.FieldRef)
}
}
}
k8scorev1.SetDefaults_ResourceList(&a.EphemeralContainerCommon.Resources.Limits)
k8scorev1.SetDefaults_ResourceList(&a.EphemeralContainerCommon.Resources.Requests)
if a.EphemeralContainerCommon.LivenessProbe != nil {
k8scorev1.SetDefaults_Probe(a.EphemeralContainerCommon.LivenessProbe)
if a.EphemeralContainerCommon.LivenessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.EphemeralContainerCommon.LivenessProbe.Handler.HTTPGet)
}
}
if a.EphemeralContainerCommon.ReadinessProbe != nil {
k8scorev1.SetDefaults_Probe(a.EphemeralContainerCommon.ReadinessProbe)
if a.EphemeralContainerCommon.ReadinessProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.EphemeralContainerCommon.ReadinessProbe.Handler.HTTPGet)
}
}
if a.EphemeralContainerCommon.StartupProbe != nil {
k8scorev1.SetDefaults_Probe(a.EphemeralContainerCommon.StartupProbe)
if a.EphemeralContainerCommon.StartupProbe.Handler.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.EphemeralContainerCommon.StartupProbe.Handler.HTTPGet)
}
}
if a.EphemeralContainerCommon.Lifecycle != nil {
if a.EphemeralContainerCommon.Lifecycle.PostStart != nil {
if a.EphemeralContainerCommon.Lifecycle.PostStart.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.EphemeralContainerCommon.Lifecycle.PostStart.HTTPGet)
}
}
if a.EphemeralContainerCommon.Lifecycle.PreStop != nil {
if a.EphemeralContainerCommon.Lifecycle.PreStop.HTTPGet != nil {
k8scorev1.SetDefaults_HTTPGetAction(a.EphemeralContainerCommon.Lifecycle.PreStop.HTTPGet)
}
}
}
}
k8scorev1.SetDefaults_ResourceList(&in.Spec.Template.Spec.Overhead)
}

func (h *MutatingHandler) setDefaultCollaSetUpdateStrategy(cls *appsv1alpha1.CollaSet) {
if cls.Spec.UpdateStrategy.PodUpdatePolicy == "" {
cls.Spec.UpdateStrategy.PodUpdatePolicy = appsv1alpha1.CollaSetInPlaceIfPossiblePodUpdateStrategyType
}

if cls.Spec.UpdateStrategy.RollingUpdate == nil {
cls.Spec.UpdateStrategy.RollingUpdate = &appsv1alpha1.RollingUpdateCollaSetStrategy{}
}

if cls.Spec.UpdateStrategy.RollingUpdate.ByPartition == nil && cls.Spec.UpdateStrategy.RollingUpdate.ByLabel == nil {
cls.Spec.UpdateStrategy.RollingUpdate.ByPartition = &appsv1alpha1.ByPartition{}
}
}

var _ inject.Client = &MutatingHandler{}

func (h *MutatingHandler) InjectClient(c client.Client) error {
h.Client = c
return nil
}

var _ admission.DecoderInjector = &MutatingHandler{}

func (h *MutatingHandler) InjectDecoder(d *admission.Decoder) error {
h.Decoder = d
return nil
}
Loading

0 comments on commit f82f2ac

Please sign in to comment.