Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
Signed-off-by: bitliu <bitliu@tencent.com>
  • Loading branch information
Xunzhuo committed Feb 23, 2024
1 parent 66d2866 commit cea05e6
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 22 deletions.
10 changes: 4 additions & 6 deletions api/v1alpha1/kubernetes_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,13 @@ func (deployment *KubernetesDeploymentSpec) ApplyMergePatch(old *appv1.Deploymen
return nil, fmt.Errorf("error marshaling original deployment: %w", err)
}

switch deployment.Patch.Type {
case StrategicMerge, "":
if deployment.Patch.Type == nil || *deployment.Patch.Type == StrategicMerge {

Check failure on line 145 in api/v1alpha1/kubernetes_helpers.go

View workflow job for this annotation

GitHub Actions / lint

ifElseChain: rewrite if-else to switch statement (gocritic)
patchedJSON, err = strategicpatch.StrategicMergePatch(originalJSON, deployment.Patch.Value.Raw, appv1.Deployment{})
case JSONMerge:
} else if *deployment.Patch.Type == JSONMerge {
patchedJSON, err = jsonpatch.MergePatch(originalJSON, deployment.Patch.Value.Raw)
default:
return nil, fmt.Errorf("unsupported merge type: %s", deployment.Patch.Type)
} else {
return nil, fmt.Errorf("unsupported merge type: %s", *deployment.Patch.Type)
}

if err != nil {
return nil, fmt.Errorf("error applying merge patch: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ type KubernetesPatchSpec struct {
//
// By default, StrategicMerge is used as the patch type.
// +optional
Type MergeType `json:"type,omitempty"`
Type *MergeType `json:"type,omitempty"`

// Object contains the raw configuration for merged object
Value apiextensionsv1.JSON `json:"value"`
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/validation/envoyproxy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func validateDeployment(spec *egv1a1.EnvoyProxySpec) []error {
if patch.Value.Raw == nil {
errs = append(errs, fmt.Errorf("envoy deployment patch object cannot be empty"))
}
if patch.Type != "" && patch.Type != egv1a1.JSONMerge && patch.Type != egv1a1.StrategicMerge {
errs = append(errs, fmt.Errorf("unsupported envoy deployment patch type %s", patch.Type))
if patch.Type != nil && *patch.Type != egv1a1.JSONMerge && *patch.Type != egv1a1.StrategicMerge {
errs = append(errs, fmt.Errorf("unsupported envoy deployment patch type %s", *patch.Type))
}
}
}
Expand Down
28 changes: 26 additions & 2 deletions api/v1alpha1/validation/envoyproxy_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

Check failure on line 19 in api/v1alpha1/validation/envoyproxy_validate_test.go

View workflow job for this annotation

GitHub Actions / lint

import 'k8s.io/utils/pointer' is not allowed from list 'main': use k8s.io/utils/ptr instead (depguard)
"k8s.io/utils/ptr"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
Expand Down Expand Up @@ -484,7 +485,7 @@ func TestValidateEnvoyProxy(t *testing.T) {
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyDeployment: &egv1a1.KubernetesDeploymentSpec{
Patch: &egv1a1.KubernetesPatchSpec{
Type: egv1a1.JSONMerge,
Type: (*egv1a1.MergeType)(pointer.String(string(egv1a1.StrategicMerge))),
},
},
},
Expand All @@ -505,7 +506,30 @@ func TestValidateEnvoyProxy(t *testing.T) {
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyDeployment: &egv1a1.KubernetesDeploymentSpec{
Patch: &egv1a1.KubernetesPatchSpec{
Type: egv1a1.JSONMerge,
Type: (*egv1a1.MergeType)(pointer.String(string(egv1a1.JSONMerge))),
Value: v1.JSON{
Raw: []byte("{}"),
},
},
},
},
},
},
},
expected: true,
}, {
name: "should valid when patch type is empty and object is not empty",
proxy: &egv1a1.EnvoyProxy{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: "test",
},
Spec: egv1a1.EnvoyProxySpec{
Provider: &egv1a1.EnvoyProxyProvider{
Type: egv1a1.ProviderTypeKubernetes,
Kubernetes: &egv1a1.EnvoyProxyKubernetesProvider{
EnvoyDeployment: &egv1a1.KubernetesDeploymentSpec{
Patch: &egv1a1.KubernetesPatchSpec{
Value: v1.JSON{
Raw: []byte("{}"),
},
Expand Down
5 changes: 5 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) {
// apply merge patch to deployment
if merged, err := deploymentConfig.ApplyMergePatch(deployment); err == nil {
deployment = merged
} else {

Check failure on line 271 in internal/infrastructure/kubernetes/proxy/resource_provider.go

View workflow job for this annotation

GitHub Actions / lint

SA9003: empty branch (staticcheck)
}

return deployment, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

Check failure on line 22 in internal/infrastructure/kubernetes/proxy/resource_provider_test.go

View workflow job for this annotation

GitHub Actions / lint

import 'k8s.io/utils/pointer' is not allowed from list 'main': use k8s.io/utils/ptr instead (depguard)
"k8s.io/utils/ptr"
"sigs.k8s.io/yaml"

Expand Down Expand Up @@ -139,7 +140,7 @@ func TestDeployment(t *testing.T) {
infra: newTestInfra(),
deploy: &egv1a1.KubernetesDeploymentSpec{
Patch: &egv1a1.KubernetesPatchSpec{
Type: egv1a1.StrategicMerge,
Type: (*egv1a1.MergeType)(pointer.String(string(egv1a1.StrategicMerge))),
Value: v1.JSON{
Raw: []byte("{\"spec\":{\"template\":{\"spec\":{\"hostNetwork\":true,\"dnsPolicy\":\"ClusterFirstWithHostNet\"}}}}"),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

Check failure on line 21 in internal/infrastructure/kubernetes/ratelimit/resource_provider_test.go

View workflow job for this annotation

GitHub Actions / lint

import 'k8s.io/utils/pointer' is not allowed from list 'main': use k8s.io/utils/ptr instead (depguard)
"k8s.io/utils/ptr"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/yaml"
Expand Down Expand Up @@ -152,7 +153,7 @@ func TestDeployment(t *testing.T) {
rateLimit: rateLimit,
deploy: &egv1a1.KubernetesDeploymentSpec{
Patch: &egv1a1.KubernetesPatchSpec{
Type: egv1a1.StrategicMerge,
Type: (*egv1a1.MergeType)(pointer.String(string(egv1a1.StrategicMerge))),
Value: v1.JSON{
Raw: []byte("{\"spec\":{\"template\":{\"spec\":{\"hostNetwork\":true,\"dnsPolicy\":\"ClusterFirstWithHostNet\"}}}}"),
},
Expand Down
9 changes: 0 additions & 9 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1595,15 +1595,6 @@ _Appears in:_



#### MergeType

_Underlying type:_ _string_

MergeType defines the type of merge operation

_Appears in:_
- [KubernetesPatchSpec](#kubernetespatchspec)



#### MetricSinkType
Expand Down

0 comments on commit cea05e6

Please sign in to comment.