Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #55 from liu-657667/feat_moduledeployment_validation
Browse files Browse the repository at this point in the history
添加moduledeployments基础属性校验,添加controller_utils单测
  • Loading branch information
gold300jin authored Sep 5, 2023
2 parents 8b06181 + 66f7818 commit b8d5602
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 5 deletions.
11 changes: 7 additions & 4 deletions module-controller/api/v1alpha1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ type ModuleTemplateSpec struct {
}

type ModuleInfo struct {
Name string `json:"name"`
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`
// +kubebuilder:validation:MinLength=1
Version string `json:"version"`
Url string `json:"url"`
Type string `json:"type,omitempty"`
Md5 string `json:"md5,omitempty"`
// +kubebuilder:validation:Format=uri
Url string `json:"url"`
Type string `json:"type,omitempty"`
Md5 string `json:"md5,omitempty"`
}

// ModuleSpec defines the desired state of Module
Expand Down
11 changes: 10 additions & 1 deletion module-controller/api/v1alpha1/moduledeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ const (
CafeDeploymentReleaseProgressTermed ReleaseProgress = "Terminated"
)

type DeployType string

const (
ModuleDeploymentDeployTypeSymmetric DeployType = "symmetric"
ModuleDeploymentDeployTypeAsymmetric DeployType = "asymmetric"
)

type ReleaseStatus struct {
// Records the latest revision.
// +optional
Expand Down Expand Up @@ -103,11 +110,13 @@ type ModuleDeploymentStrategy struct {
type ModuleDeploymentSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// +kubebuilder:validation:MinLength=1
BaseDeploymentName string `json:"baseDeploymentName"`

Template ModuleTemplateSpec `json:"template,omitempty"`

DeployType string `json:"deployType"`
// +kubebuilder:validation:Enum={"symmetric","asymmetric"}
DeployType DeployType `json:"deployType"`

Replicas int32 `json:"replicas,omitempty"`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ spec:
baseDeploymentName:
description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Important: Run "make" to regenerate code after modifying this file'
minLength: 1
type: string
deployType:
enum:
- symmetric
- asymmetric
type: string
minReadySeconds:
format: int32
Expand Down Expand Up @@ -93,12 +97,15 @@ spec:
md5:
type: string
name:
minLength: 1
type: string
type:
type: string
url:
format: uri
type: string
version:
minLength: 1
type: string
required:
- name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ spec:
md5:
type: string
name:
minLength: 1
type: string
type:
type: string
url:
format: uri
type: string
version:
minLength: 1
type: string
required:
- name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ spec:
md5:
type: string
name:
minLength: 1
type: string
type:
type: string
url:
format: uri
type: string
version:
minLength: 1
type: string
required:
- name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ spec:
md5:
type: string
name:
minLength: 1
type: string
type:
type: string
url:
format: uri
type: string
version:
minLength: 1
type: string
required:
- name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sofastack/sofa-serverless/api/v1alpha1"
moduledeploymentv1alpha1 "github.com/sofastack/sofa-serverless/api/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -97,6 +98,7 @@ func prepareModuleDeployment(namespace, moduleDeploymentName string) v1alpha1.Mo
moduleDeployment := v1alpha1.ModuleDeployment{
Spec: v1alpha1.ModuleDeploymentSpec{
BaseDeploymentName: baseDeploymentName,
DeployType: moduledeploymentv1alpha1.ModuleDeploymentDeployTypeSymmetric,
Template: v1alpha1.ModuleTemplateSpec{
Spec: v1alpha1.ModuleSpec{
Module: v1alpha1.ModuleInfo{
Expand Down
68 changes: 68 additions & 0 deletions module-controller/internal/utils/controller_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package utils

import (
"github.com/sofastack/sofa-serverless/internal/constants/finalizer"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"testing"
"time"
)

func TestAddNotExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
assert.True(t, AddFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestHasNotExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
assert.False(t, HasFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestRemoveNotExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
assert.False(t, RemoveFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestAddExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer}
assert.False(t, AddFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestHasExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer}
assert.True(t, HasFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestRemoveExistedFinalizer(t *testing.T) {
meta := &metav1.ObjectMeta{}
meta.Finalizers = []string{finalizer.ModuleReplicaSetExistedFinalizer}
assert.True(t, RemoveFinalizer(meta, finalizer.ModuleReplicaSetExistedFinalizer))
}

func TestKeyEqual(t *testing.T) {
request := ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "default", Name: "test"}}
key := Key(request)
assert.Equal(t, "default/test", key)
}

func TestGetNextReconcileTime(t *testing.T) {
now := time.Now()
reconcileTime := GetNextReconcileTime(now)
assert.Equal(t, "10s", reconcileTime.String())

m, _ := time.ParseDuration("-11m")
reconcileTime2 := GetNextReconcileTime(now.Add(m))
assert.Equal(t, "1m0s", reconcileTime2.String())

m31, _ := time.ParseDuration("-31m")
reconcileTime3 := GetNextReconcileTime(now.Add(m31))
assert.Equal(t, "5m0s", reconcileTime3.String())

h1, _ := time.ParseDuration("-61m")
reconcileTime4 := GetNextReconcileTime(now.Add(h1))
assert.Equal(t, "10m0s", reconcileTime4.String())
}

0 comments on commit b8d5602

Please sign in to comment.