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

test: add some tests #118

Closed
wants to merge 4 commits into from
Closed
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![Operating](https://github.com/KusionStack/operating/actions/workflows/release.yaml/badge.svg)](https://github.com/KusionStack/operating/actions/workflows/release.yaml)
[![GitHub release](https://img.shields.io/github/release/KusionStack/operating.svg)](https://github.com/KusionStack/operating/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/KusionStack/operating)](https://goreportcard.com/report/github.com/KusionStack/operating)
[![codecov](https://codecov.io/gh/KusionStack/operating/branch/master/graph/badge.svg)](https://codecov.io/gh/KusionStack/operating)

KusionStack Operating ([official site](https://kusionstack.io/docs/operating/introduction/)) provides a set of workloads and operators
built on Kubernetes Custom Resource Definitions (CRDs), with a primary aim of bridging the gap
Expand Down
253 changes: 253 additions & 0 deletions apis/apps/v1alpha1/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
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 v1alpha1

import (
k8scorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
)

func SetDetaultCollaSet(cls *CollaSet) {
SetDefaultPodSpec(cls)
SetDefaultCollaSetUpdateStrategy(cls)
}

func SetDefaultPodSpec(in *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 SetDefaultCollaSetUpdateStrategy(cls *CollaSet) {
if cls.Spec.UpdateStrategy.PodUpdatePolicy == "" {
cls.Spec.UpdateStrategy.PodUpdatePolicy = CollaSetInPlaceIfPossiblePodUpdateStrategyType
}

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

if cls.Spec.UpdateStrategy.RollingUpdate.ByPartition == nil && cls.Spec.UpdateStrategy.RollingUpdate.ByLabel == nil {
cls.Spec.UpdateStrategy.RollingUpdate.ByPartition = &ByPartition{}
}
}
4 changes: 2 additions & 2 deletions pkg/controllers/collaset/collaset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func calculateStatus(instance *appsv1alpha1.CollaSet, newStatus *appsv1alpha1.Co
replicas++

isUpdated := false
if isUpdated = controllerutils.IsPodUpdatedRevision(podWrapper.Pod, updatedRevision.Name); isUpdated {
if isUpdated = utils.IsPodUpdatedRevision(podWrapper.Pod, updatedRevision.Name); isUpdated {
updatedReplicas++
}

Expand All @@ -229,7 +229,7 @@ func calculateStatus(instance *appsv1alpha1.CollaSet, newStatus *appsv1alpha1.Co
}
}

if controllerutils.IsServiceAvailable(podWrapper.Pod) {
if controllerutils.IsPodServiceAvailable(podWrapper.Pod) {
availableReplicas++
if isUpdated {
updatedAvailableReplicas++
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/collaset/collaset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("collaset controller", func() {
Eventually(func() bool {
Expect(c.Get(context.TODO(), types.NamespacedName{Namespace: podToDelete.Namespace, Name: podToDelete.Name}, podToDelete)).Should(BeNil())
return podToDelete.DeletionTimestamp != nil
}, 500*time.Second, 1*time.Second).Should(BeTrue())
}, 5*time.Second, 1*time.Second).Should(BeTrue())

// there should be 3 pods and one of them is terminating
Eventually(func() bool {
Expand Down
13 changes: 3 additions & 10 deletions pkg/controllers/collaset/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

appsalphav1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/utils/revision"
)

func getCollaSetPatch(cls *appsalphav1.CollaSet) ([]byte, error) {
Expand Down Expand Up @@ -52,6 +53,8 @@ func getCollaSetPatch(cls *appsalphav1.CollaSet) ([]byte, error) {
return patch, err
}

var _ revision.OwnerAdapter = &revisionOwnerAdapter{}

type revisionOwnerAdapter struct {
}

Expand All @@ -75,16 +78,6 @@ func (roa *revisionOwnerAdapter) GetPatch(obj metav1.Object) ([]byte, error) {
return getCollaSetPatch(cs)
}

func (roa *revisionOwnerAdapter) GetSelectorLabels(obj metav1.Object) map[string]string {
ips, _ := obj.(*appsalphav1.CollaSet)
labels := map[string]string{}
for k, v := range ips.Spec.Template.Labels {
labels[k] = v
}

return labels
}

func (roa *revisionOwnerAdapter) GetCurrentRevision(obj metav1.Object) string {
ips, _ := obj.(*appsalphav1.CollaSet)
return ips.Status.CurrentRevision
Expand Down
3 changes: 1 addition & 2 deletions pkg/controllers/collaset/synccontrol/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"sort"

collasetutils "kusionstack.io/operating/pkg/controllers/collaset/utils"
controllerutils "kusionstack.io/operating/pkg/controllers/utils"
"kusionstack.io/operating/pkg/controllers/utils/podopslifecycle"
)

Expand Down Expand Up @@ -52,5 +51,5 @@ func (s ActivePodsForDeletion) Less(i, j int) bool {
return false
}

return controllerutils.ComparePod(l.Pod, r.Pod)
return collasetutils.ComparePod(l.Pod, r.Pod)
}
6 changes: 3 additions & 3 deletions pkg/controllers/collaset/synccontrol/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

appsv1alpha1 "kusionstack.io/operating/apis/apps/v1alpha1"
"kusionstack.io/operating/pkg/controllers/collaset/utils"
collasetutils "kusionstack.io/operating/pkg/controllers/collaset/utils"
controllerutils "kusionstack.io/operating/pkg/controllers/utils"
"kusionstack.io/operating/pkg/controllers/utils/podopslifecycle"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func (o orderByDefault) Less(i, j int) bool {
return false
}

return controllerutils.ComparePod(l.Pod, r.Pod)
return utils.ComparePod(l.Pod, r.Pod)
}

type PodUpdater interface {
Expand Down Expand Up @@ -191,7 +191,7 @@ func (u *InPlaceIfPossibleUpdater) AnalyseAndGetUpdatedPod(cls *appsv1alpha1.Col
}

inPlaceUpdateSupport = true
updatedPod, err = controllerutils.PatchToPod(currentPod, updatedPod, podUpdateInfo.Pod)
updatedPod, err = utils.PatchToPod(currentPod, updatedPod, podUpdateInfo.Pod)

if onlyMetadataChanged {
if updatedPod.Annotations != nil {
Expand Down
Loading
Loading