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

fix: set default values of CRDs #99

Merged
merged 6 commits into from
Sep 5, 2022
Merged
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
4 changes: 4 additions & 0 deletions apis/namespacedingress/v1alpha1/namespacedingress_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ type NamespacedIngressSpec struct {
// +optional
PodAnnotations map[string]string `json:"podAnnotations,omitempty"`

// +kubebuilder:default={enabled: true, port: {name: http, protocol: TCP, port: 80, targetPort: 8000}}

// The http configuration of this ingress controller.
// +optional
HTTP HTTP `json:"http,omitempty"`

// +kubebuilder:default={enabled: false, port: {name: https, protocol: TCP, port: 443, targetPort: 8443}, sslPassthrough: {enabled: false, upstreamPort: 443}}

// TLS is the configuration of TLS of this ingress controller
// +optional
TLS TLS `json:"tls,omitempty"`
Expand Down
17 changes: 17 additions & 0 deletions charts/fsm/crds/flomesh.io_namespacedingresses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,13 @@ spec:
type: object
type: array
http:
default:
enabled: true
port:
name: http
port: 80
protocol: TCP
targetPort: 8000
description: The http configuration of this ingress controller.
properties:
enabled:
Expand Down Expand Up @@ -1110,6 +1117,16 @@ spec:
an Ingress the most used types are NodePort, and LoadBalancer
type: string
tls:
default:
enabled: false
port:
name: https
port: 443
protocol: TCP
targetPort: 8443
sslPassthrough:
enabled: false
upstreamPort: 443
description: TLS is the configuration of TLS of this ingress controller
properties:
enabled:
Expand Down
7 changes: 3 additions & 4 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"reflect"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -172,7 +171,7 @@ func createOrUpdateUnstructured(ctx context.Context, c client.Client, obj *unstr
result := controllerutil.OperationResultNone
if !reflect.DeepEqual(obj, modifiedObj) {
klog.V(5).Infof("Patching Object %s ...", key)
patchData, err := client.Apply.Data(modifiedObj)
patchData, err := client.Merge.Data(modifiedObj)
if err != nil {
klog.Errorf("Create ApplyPatch err: %s", err)
return controllerutil.OperationResultNone, err
Expand All @@ -182,8 +181,8 @@ func createOrUpdateUnstructured(ctx context.Context, c client.Client, obj *unstr
if err := c.Patch(
ctx,
obj,
client.RawPatch(types.ApplyPatchType, patchData),
&client.PatchOptions{FieldManager: "fsm", Force: pointer.Bool(true)},
client.RawPatch(types.MergePatchType, patchData),
&client.PatchOptions{FieldManager: "fsm"},
); err != nil {
klog.Errorf("Patch Object %s err: %s", key, err)
return result, err
Expand Down
17 changes: 11 additions & 6 deletions pkg/webhooks/cluster/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
admissionregv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
)

const (
Expand Down Expand Up @@ -120,15 +121,14 @@ func (w *ClusterDefaulter) SetDefaults(obj interface{}) {
}

if c.Spec.Replicas == nil {
c.Spec.Replicas = defaultReplicas()
c.Spec.Replicas = pointer.Int32(1)
}

klog.V(4).Infof("After setting default values, spec=%#v", c.Spec)
}
if c.Spec.LogLevel == 0 {
c.Spec.LogLevel = 2
}

func defaultReplicas() *int32 {
var r int32 = 1
return &r
klog.V(4).Infof("After setting default values, spec=%#v", c.Spec)
}

type ClusterValidator struct {
Expand Down Expand Up @@ -169,6 +169,11 @@ func (w *ClusterValidator) ValidateCreate(obj interface{}) error {
}
}

if !cluster.Spec.IsInCluster &&
(cluster.Spec.Kubeconfig == "" || cluster.Spec.Gateway == "" || cluster.Spec.ControlPlaneRepoRootUrl == "") {
return errors.New("spec.Kubeconfig, spec.Gateway & spec.ControlPlaneRepoRootUrl are required if spec.IsInCluster is false")
}

return doValidation(obj)
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/webhooks/namespacedingress/namespacedingress_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
admissionregv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
)

const (
Expand Down Expand Up @@ -116,6 +117,18 @@ func (w *NamespacedIngressDefaulter) SetDefaults(obj interface{}) {
c.Spec.ServiceAccountName = "fsm-namespaced-ingress"
}

if c.Spec.LogLevel == 0 {
c.Spec.LogLevel = 2
}

if c.Spec.Replicas == nil {
c.Spec.Replicas = pointer.Int32(1)
}

if c.Spec.TLS.SSLPassthrough.UpstreamPort == 0 {
c.Spec.TLS.SSLPassthrough.UpstreamPort = 443
}

klog.V(4).Infof("After setting default values, spec=%#v", c.Spec)
}

Expand Down