From dd836fc6524a53dabd5be4a38f7da15b748e8c1a Mon Sep 17 00:00:00 2001 From: hongzhouzi Date: Wed, 11 May 2022 10:50:33 +0800 Subject: [PATCH] fix:modify the default resource reservation of gateway system, gateway.go typo --- Makefile | 2 +- .../crds/gateway.kubesphere.io_gateways.yaml | 27 +++++++++++++++++++ config/gateway/templates/nginx-ingress.yaml | 8 +----- config/gateway/values.yaml | 8 ++++++ pkg/models/gateway/gateway.go | 25 +++++++++-------- .../api/gateway/v1alpha1/gateway_types.go | 6 +++-- .../gateway/v1alpha1/zz_generated.deepcopy.go | 8 ++++++ 7 files changed, 63 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index ef4b48684..002cfc372 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CRD_OPTIONS ?= "crd:trivialVersions=true" GV="network:v1alpha1 servicemesh:v1alpha2 tenant:v1alpha1 tenant:v1alpha2 devops:v1alpha1 iam:v1alpha2 devops:v1alpha3 cluster:v1alpha1 storage:v1alpha1 auditing:v1alpha1 types:v1beta1 quota:v1alpha2 application:v1alpha1 notification:v2beta1 gateway:v1alpha1" -MANIFESTS="application/* cluster/* iam/* network/v1alpha1 quota/* storage/* tenant/*" +MANIFESTS="application/* cluster/* iam/* network/v1alpha1 quota/* storage/* tenant/* gateway/*" # App Version APP_VERSION = v3.2.0 diff --git a/config/crds/gateway.kubesphere.io_gateways.yaml b/config/crds/gateway.kubesphere.io_gateways.yaml index a4658c01a..28c8212c5 100644 --- a/config/crds/gateway.kubesphere.io_gateways.yaml +++ b/config/crds/gateway.kubesphere.io_gateways.yaml @@ -66,6 +66,33 @@ spec: replicas: format: int32 type: integer + resources: + description: ResourceRequirements describes the compute resource + requirements. + properties: + limits: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'Limits describes the maximum amount of compute + resources allowed. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/' + type: object + requests: + additionalProperties: + anyOf: + - type: integer + - type: string + pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$ + x-kubernetes-int-or-string: true + description: 'Requests describes the minimum amount of compute + resources required. If Requests is omitted for a container, + it defaults to Limits if that is explicitly specified, otherwise + to an implementation-defined value. More info: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/' + type: object + type: object type: object service: properties: diff --git a/config/gateway/templates/nginx-ingress.yaml b/config/gateway/templates/nginx-ingress.yaml index 1dd035bf8..3250c3394 100644 --- a/config/gateway/templates/nginx-ingress.yaml +++ b/config/gateway/templates/nginx-ingress.yaml @@ -192,13 +192,7 @@ spec: # ref: https://github.com/kubernetes/ingress-nginx/issues/4735#issuecomment-551204903 # Ideally, there should be no limits. # https://engineering.indeedblog.com/blog/2019/12/cpu-throttling-regression-fix/ - resources: - # limits: - # cpu: 100m - # memory: 90Mi - requests: - cpu: 100m - memory: 90Mi + resources: {{ toYaml .Values.deployment.resources | nindent 6 }} # Mutually exclusive with keda autoscaling autoscaling: diff --git a/config/gateway/values.yaml b/config/gateway/values.yaml index b6a83da4e..9276a485e 100644 --- a/config/gateway/values.yaml +++ b/config/gateway/values.yaml @@ -26,4 +26,12 @@ service: deployment: annotations: {} replicas: 1 + resources: +# limits: +# cpu: 100m +# memory: 90Mi + requests: + cpu: 100m + memory: 90Mi + \ No newline at end of file diff --git a/pkg/models/gateway/gateway.go b/pkg/models/gateway/gateway.go index 240581785..9eb5ddfb1 100644 --- a/pkg/models/gateway/gateway.go +++ b/pkg/models/gateway/gateway.go @@ -93,9 +93,9 @@ func (c *gatewayOperator) getWorkingNamespace(namespace string) string { return ns } -// overide user's setting when create/update a project gateway. -func (c *gatewayOperator) overideDefaultValue(gateway *v1alpha1.Gateway, namespace string) *v1alpha1.Gateway { - // overide default name +// override user's setting when create/update a project gateway. +func (c *gatewayOperator) overrideDefaultValue(gateway *v1alpha1.Gateway, namespace string) *v1alpha1.Gateway { + // override default name gateway.Name = fmt.Sprint(gatewayPrefix, namespace) if gateway.Name != globalGatewayname { gateway.Spec.Controller.Scope = v1alpha1.Scope{Enabled: true, Namespace: namespace} @@ -174,6 +174,9 @@ func (c *gatewayOperator) convert(namespace string, svc *corev1.Service, deploy legacy.Spec.Deployment.Annotations = make(map[string]string) legacy.Spec.Deployment.Annotations[SidecarInject] = an } + if len(deploy.Spec.Template.Spec.Containers) > 0 { + legacy.Spec.Deployment.Resources = deploy.Spec.Template.Spec.Containers[0].Resources + } return &legacy } @@ -201,7 +204,7 @@ func (c *gatewayOperator) getMasterNodeIp() []string { } func (c *gatewayOperator) updateStatus(gateway *v1alpha1.Gateway, svc *corev1.Service) (*v1alpha1.Gateway, error) { - // append selected node ip as loadbalancer ingress ip + // append selected node ip as loadBalancer ingress ip if svc.Spec.Type != corev1.ServiceTypeLoadBalancer && len(svc.Status.LoadBalancer.Ingress) == 0 { rips := c.getMasterNodeIp() for _, rip := range rips { @@ -240,8 +243,8 @@ func (c *gatewayOperator) updateStatus(gateway *v1alpha1.Gateway, svc *corev1.Se return gateway, nil } -// GetGateways returns all Gateways from the project. There are at most 2 gatways exists in a project, -// a Glabal Gateway and a Project Gateway or a Legacy Project Gateway. +// GetGateways returns all Gateways from the project. There are at most 2 gateways exists in a project, +// a Global Gateway and a Project Gateway or a Legacy Project Gateway. func (c *gatewayOperator) GetGateways(namespace string) ([]*v1alpha1.Gateway, error) { var gateways []*v1alpha1.Gateway @@ -295,7 +298,7 @@ func (c *gatewayOperator) CreateGateway(namespace string, obj *v1alpha1.Gateway) return nil, fmt.Errorf("can't create project gateway if legacy gateway exists, please upgrade the gateway firstly") } - c.overideDefaultValue(obj, namespace) + c.overrideDefaultValue(obj, namespace) err := c.client.Create(context.TODO(), obj) return obj, err } @@ -316,7 +319,7 @@ func (c *gatewayOperator) UpdateGateway(namespace string, obj *v1alpha1.Gateway) if c.options.Namespace == "" && obj.Namespace != namespace || c.options.Namespace != "" && c.options.Namespace != obj.Namespace { return nil, fmt.Errorf("namepsace doesn't match with origin namesapce") } - c.overideDefaultValue(obj, namespace) + c.overrideDefaultValue(obj, namespace) err := c.client.Update(context.TODO(), obj) return obj, err } @@ -332,7 +335,7 @@ func (c *gatewayOperator) UpgradeGateway(namespace string) (*v1alpha1.Gateway, e return nil, fmt.Errorf("invalid operation, can't upgrade legacy gateway when working namespace changed") } - // Get legency gateway's config from configmap + // Get legacy gateway's config from configmap cm := &corev1.ConfigMap{} err := c.client.Get(context.TODO(), client.ObjectKey{Namespace: l.Namespace, Name: fmt.Sprintf("%s-nginx", l.Name)}, cm) if err == nil { @@ -355,7 +358,7 @@ func (c *gatewayOperator) UpgradeGateway(namespace string) (*v1alpha1.Gateway, e return nil, err } - // Patch the legacy Serivce with helm annotations, So that it can be mannaged by the helm release. + // Patch the legacy Service with helm annotations, So that it can be managed by the helm release. patch := []byte(fmt.Sprintf(helmPatch, l.Name, l.Namespace)) err = c.client.Patch(context.Background(), &corev1.Service{ ObjectMeta: v1.ObjectMeta{ @@ -368,7 +371,7 @@ func (c *gatewayOperator) UpgradeGateway(namespace string) (*v1alpha1.Gateway, e return nil, err } - c.overideDefaultValue(l, namespace) + c.overrideDefaultValue(l, namespace) err = c.client.Create(context.TODO(), l) return l, err } diff --git a/staging/src/kubesphere.io/api/gateway/v1alpha1/gateway_types.go b/staging/src/kubesphere.io/api/gateway/v1alpha1/gateway_types.go index 8c461b806..ca1687048 100644 --- a/staging/src/kubesphere.io/api/gateway/v1alpha1/gateway_types.go +++ b/staging/src/kubesphere.io/api/gateway/v1alpha1/gateway_types.go @@ -31,7 +31,7 @@ type GatewaySpec struct { type ControllerSpec struct { // +optional - Replicas *int32 `json:"replicas,omitempty" protobuf:"varint,1,opt,name=replicas"` + Replicas *int32 `json:"replicas,omitempty"` // +optional Annotations map[string]string `json:"annotations,omitempty"` // +optional @@ -49,9 +49,11 @@ type ServiceSpec struct { type DeploymentSpec struct { // +optional - Replicas *int32 `json:"replicas,omitempty" protobuf:"varint,1,opt,name=replicas"` + Replicas *int32 `json:"replicas,omitempty"` // +optional Annotations map[string]string `json:"annotations,omitempty"` + // +optional + Resources corev1.ResourceRequirements `json:"resources,omitempty"` } type Scope struct { diff --git a/staging/src/kubesphere.io/api/gateway/v1alpha1/zz_generated.deepcopy.go b/staging/src/kubesphere.io/api/gateway/v1alpha1/zz_generated.deepcopy.go index 733868269..61bdcf2d3 100644 --- a/staging/src/kubesphere.io/api/gateway/v1alpha1/zz_generated.deepcopy.go +++ b/staging/src/kubesphere.io/api/gateway/v1alpha1/zz_generated.deepcopy.go @@ -40,6 +40,13 @@ func (in *ControllerSpec) DeepCopyInto(out *ControllerSpec) { (*out)[key] = val } } + if in.Config != nil { + in, out := &in.Config, &out.Config + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } out.Scope = in.Scope } @@ -68,6 +75,7 @@ func (in *DeploymentSpec) DeepCopyInto(out *DeploymentSpec) { (*out)[key] = val } } + in.Resources.DeepCopyInto(&out.Resources) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DeploymentSpec.