Skip to content

Commit

Permalink
refactor: moves the admission-webhook code into pkg/webhook from api (#…
Browse files Browse the repository at this point in the history
…516)

* refactor: moves the admission-webhook code into pkg/webhook from api

* fix: script & tests

* chore: fix go-lint

---------

Co-authored-by: Aylei <[email protected]>
  • Loading branch information
cyberchen98 and aylei authored Jun 26, 2024
1 parent b6ba4b9 commit d98a533
Show file tree
Hide file tree
Showing 27 changed files with 1,012 additions and 749 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GOPROXY ?= "https://proxy.golang.org,direct"
MO_VERSION ?= "nightly-d98832bb"
MO_IMAGE_REPO ?= "matrixorigin/matrixone"
BRANCH ?= main
ENVTEST_K8S_VERSION = 1.24.1

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -86,12 +87,23 @@ go-lint: golangci-lint
check-license: license-eye
$(LICENSE_EYE) -v info -c .licenserc.yml header check

LOCALBIN ?= $(shell pwd)/api/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

ENVTEST ?= $(LOCALBIN)/setup-envtest

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

# TODO: include E2E
test: api-test unit

# Run unit tests
unit: generate fmt vet manifests
CGO_ENABLED=0 go test ./pkg/... -coverprofile cover.out
unit: generate fmt vet manifests envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" CGO_ENABLED=0 go test ./pkg/... -coverprofile cover.out

api-test:
cd api && make test
Expand Down
136 changes: 0 additions & 136 deletions api/core/v1alpha1/cnset_webhook.go

This file was deleted.

53 changes: 0 additions & 53 deletions api/core/v1alpha1/common_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ const (
reasonEmpty = "empty"
)

var (
// ServiceDefaultArgs is a cache variable for default args, should be read only in this package
ServiceDefaultArgs *DefaultArgs
)

func (c *ConditionalStatus) SetCondition(condition metav1.Condition) {
if c.Conditions == nil {
c.Conditions = []metav1.Condition{}
Expand Down Expand Up @@ -215,54 +210,6 @@ type DefaultArgs struct {
Proxy []string `json:"proxy,omitempty"`
}

// setDefaultServiceArgs set default args for service, we only set default args when there is service args config in service spec
func setDefaultServiceArgs(object interface{}) {
if ServiceDefaultArgs == nil {
return
}
switch obj := object.(type) {
case *LogSetSpec:
// set default arguments only when user does not set any arguments
if len(obj.ServiceArgs) == 0 {
obj.ServiceArgs = ServiceDefaultArgs.LogService
}
case *DNSetSpec:
if len(obj.ServiceArgs) == 0 {
obj.ServiceArgs = ServiceDefaultArgs.DN
}
case *CNSetSpec:
if len(obj.ServiceArgs) == 0 {
obj.ServiceArgs = ServiceDefaultArgs.CN
}
case *ProxySetSpec:
if len(obj.ServiceArgs) == 0 {
obj.ServiceArgs = ServiceDefaultArgs.Proxy
}
default:
moLog.Error(fmt.Errorf("unknown type:%T", object), "expected types: *LogSetSpec, *DNSetSpec, *CNSetSpec")
return
}
}

// setPodSetDefaults set default values in pod set
func setPodSetDefaults(s *PodSet) {
if s.Overlay == nil {
s.Overlay = &Overlay{}
}
s.Overlay.Env = appendIfNotExist(s.Overlay.Env, corev1.EnvVar{Name: EnvGoDebug, Value: DefaultGODebug}, func(v corev1.EnvVar) string {
return v.Name
})
}

func appendIfNotExist[K comparable, V any](list []V, elem V, keyFunc func(V) K) []V {
for _, o := range list {
if keyFunc(o) == keyFunc(elem) {
return list
}
}
return append(list, elem)
}

func GetCNPodUUID(pod *corev1.Pod) string {
addr := fmt.Sprintf("%s.%s.%s.svc\n", pod.Name, pod.Spec.Subdomain, pod.Namespace)
sum := sha256.Sum256([]byte(addr))
Expand Down
99 changes: 0 additions & 99 deletions api/core/v1alpha1/dnset_webhook.go

This file was deleted.

32 changes: 32 additions & 0 deletions api/core/v1alpha1/logset_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,35 @@ func (l *LogSet) AsDependency() LogSetRef {
},
}
}

// setDefaultRetentionPolicy always set PVCRetentionPolicy, and always set S3RetentionPolicy only if S3 is not nil
// setDefaultRetentionPolicy does not change origin policy and only set default value when policy is nil
func (l *LogSetSpec) setDefaultRetentionPolicy() {
defaultDeletePolicy := PVCRetentionPolicyDelete

if l.SharedStorage.S3 == nil {
if l.PVCRetentionPolicy == nil {
l.PVCRetentionPolicy = &defaultDeletePolicy
}
return
}

pvcPolicy := l.PVCRetentionPolicy
s3Policy := l.SharedStorage.S3.S3RetentionPolicy

switch {
// if both set, does not set any values
case pvcPolicy != nil && s3Policy != nil:
return
// if both not set, set to delete
case pvcPolicy == nil && s3Policy == nil:
l.PVCRetentionPolicy = &defaultDeletePolicy
l.SharedStorage.S3.S3RetentionPolicy = &defaultDeletePolicy
// if only set pvcPolicy, set it to s3Policy
case pvcPolicy != nil && s3Policy == nil:
l.SharedStorage.S3.S3RetentionPolicy = pvcPolicy
// if only set s3Policy, set it to pvcPolicy
case pvcPolicy == nil && s3Policy != nil:
l.PVCRetentionPolicy = s3Policy
}
}
5 changes: 5 additions & 0 deletions api/core/v1alpha1/logset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v1alpha1
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
)

const (
Expand All @@ -31,6 +32,10 @@ const (
FailedPodStrategyDelete FailedPodStrategy = "Delete"
)

const (
defaultStoreFailureTimeout = 10 * time.Minute
)

type LogSetSpec struct {
PodSet `json:",inline"`

Expand Down
Loading

0 comments on commit d98a533

Please sign in to comment.