From aee41f0cb85f0315e6e8706b1c6c3db5bb8583d9 Mon Sep 17 00:00:00 2001 From: Bin Liu Date: Mon, 5 Dec 2022 13:55:32 +0800 Subject: [PATCH] make antreaConfig structs public Signed-off-by: Bin Liu --- .../controllers/antrea/antreaconfig_util.go | 66 +++++++++---------- .../antreaconfig_controller_test.go | 49 ++++++++------ 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/addons/controllers/antrea/antreaconfig_util.go b/addons/controllers/antrea/antreaconfig_util.go index 3af9a009650..dcbbaba05bd 100644 --- a/addons/controllers/antrea/antreaconfig_util.go +++ b/addons/controllers/antrea/antreaconfig_util.go @@ -20,33 +20,27 @@ import ( ) // AntreaConfigSpec defines the desired state of AntreaConfig -type antreaConfigSpec struct { +type AntreaConfigSpec struct { InfraProvider string `yaml:"infraProvider"` - Antrea antrea `yaml:"antrea,omitempty"` - AntreaNsx antreaNsx `yaml:"antreaNsx,omitempty"` + Antrea Antrea `yaml:"antrea,omitempty"` + AntreaNsx AntreaNsx `yaml:"antreaNsx,omitempty"` } -type antrea struct { +type Antrea struct { AntreaConfigDataValue antreaConfigDataValue `yaml:"config,omitempty"` } -type antreaNsx struct { +type AntreaNsx struct { Enable bool `yaml:"enable,omitempty"` BootstrapFrom AntreaNsxBootstrapFrom `yaml:"bootstrapFrom,omitempty"` - AntreaNsxConfig antreaNsxConfig `yaml:"config,omitempty"` -} - -type antreaNsxProvider struct { - ApiVersion string `yaml:"apiVersion,omitempty"` - Kind string `yaml:"kind,omitempty"` - Name string `yaml:"kind,omitempty"` + AntreaNsxConfig AntreaNsxConfig `yaml:"config,omitempty"` } type AntreaNsxBootstrapFrom struct { // ProviderRef is used with uTKG, which will be filled by NCP operator - ProviderRef *antreaNsxProvider `yaml:"providerRef,omitempty"` + ProviderRef *AntreaNsxProvider `yaml:"providerRef,omitempty"` // Inline is used with TKGm, user need to fill in manually - Inline *antreaNsxInline `yaml:"inline,omitempty"` + Inline *AntreaNsxInline `yaml:"inline,omitempty"` } type AntreaNsxProvider struct { @@ -58,66 +52,66 @@ type AntreaNsxProvider struct { Name string `yaml:"name,omitempty"` } -type nsxCertRef struct { +type NsxCertRef struct { // TLSCert is cert file to access nsx manager TLSCert string `yaml:"tls.crt,omitempty"` // TLSKey is key file to access nsx manager TLSKey string `yaml:"tls.key,omitempty"` } -type antreaNsxInline struct { +type AntreaNsxInline struct { NsxManagers []string `yaml:"nsxManagers,omitempty"` ClusterName string `yaml:"clusterName,omitempty"` - NsxCertRef nsxCertRef `yaml:"NsxCert,omitempty"` + NsxCertRef NsxCertRef `yaml:"NsxCert,omitempty"` } -type antreaNsxConfig struct { +type AntreaNsxConfig struct { InfraType string `yaml:"infraType,omitempty"` } -type antreaEgress struct { +type AntreaEgress struct { EgressExceptCIDRs []string `yaml:"exceptCIDRs,omitempty"` } -type antreaNodePortLocal struct { +type AntreaNodePortLocal struct { Enabled bool `yaml:"enabled,omitempty"` PortRange string `yaml:"portRange,omitempty"` } -type antreaProxy struct { +type AntreaProxy struct { ProxyAll bool `yaml:"proxyAll,omitempty"` NodePortAddresses []string `yaml:"nodePortAddresses,omitempty"` SkipServices []string `yaml:"skipServices,omitempty"` ProxyLoadBalancerIPs bool `yaml:"proxyLoadBalancerIPs,omitempty"` } -type antreaFlowExporter struct { +type AntreaFlowExporter struct { CollectorAddress string `yaml:"collectorAddress,omitempty"` PollInterval string `yaml:"pollInterval,omitempty"` ActiveFlowTimeout string `yaml:"activeFlowTimeout,omitempty"` IdleFlowTimeout string `yaml:"idleFlowTimeout,omitempty"` } -type antreaMultiCluster struct { +type AntreaMultiCluster struct { Enable bool `yaml:"enable,omitempty"` Namespace string `yaml:"namespace,omitempty"` } -type antreaMulticast struct { +type AntreaMulticast struct { IGMPQueryInterval string `yaml:"igmpQueryInterval,omitempty"` } -type antreaWireGuard struct { +type AntreaWireGuard struct { Port int `yaml:"port,omitempty"` } type antreaConfigDataValue struct { - Egress antreaEgress `yaml:"egress,omitempty"` - NodePortLocal antreaNodePortLocal `yaml:"nodePortLocal,omitempty"` - AntreaProxy antreaProxy `yaml:"antreaProxy,omitempty"` - FlowExporter antreaFlowExporter `yaml:"flowExporter,omitempty"` - Multicast antreaMulticast `yaml:"multicast,omitempty"` - MultiCluster antreaMultiCluster `yaml:"multicluster,omitempty"` + Egress AntreaEgress `yaml:"egress,omitempty"` + NodePortLocal AntreaNodePortLocal `yaml:"nodePortLocal,omitempty"` + AntreaProxy AntreaProxy `yaml:"antreaProxy,omitempty"` + FlowExporter AntreaFlowExporter `yaml:"flowExporter,omitempty"` + Multicast AntreaMulticast `yaml:"multicast,omitempty"` + MultiCluster AntreaMultiCluster `yaml:"multicluster,omitempty"` KubeAPIServerOverride string `yaml:"kubeAPIServerOverride,omitempty"` TransportInterface string `yaml:"transportInterface,omitempty"` TransportInterfaceCIDRs []string `yaml:"transportInterfaceCIDRs,omitempty"` @@ -125,7 +119,7 @@ type antreaConfigDataValue struct { TunnelType string `yaml:"tunnelType,omitempty"` TrafficEncryptionMode string `yaml:"trafficEncryptionMode,omitempty"` EnableUsageReporting bool `yaml:"enableUsageReporting,omitempty"` - WireGuard antreaWireGuard `yaml:"wireGuard,omitempty"` + WireGuard AntreaWireGuard `yaml:"wireGuard,omitempty"` ServiceCIDR string `yaml:"serviceCIDR,omitempty"` ServiceCIDRv6 string `yaml:"serviceCIDRv6,omitempty"` TrafficEncapMode string `yaml:"trafficEncapMode,omitempty"` @@ -136,10 +130,10 @@ type antreaConfigDataValue struct { EnableBridgingMode bool `yaml:"enableBridgingMode,omitempty"` DisableTXChecksumOffload bool `yaml:"disableTXChecksumOffload,omitempty"` DNSServerOverride string `yaml:"dnsServerOverride,omitempty"` - FeatureGates antreaFeatureGates `yaml:"featureGates,omitempty"` + FeatureGates AntreaFeatureGates `yaml:"featureGates,omitempty"` } -type antreaFeatureGates struct { +type AntreaFeatureGates struct { AntreaProxy bool `yaml:"AntreaProxy"` EndpointSlice bool `yaml:"EndpointSlice"` AntreaPolicy bool `yaml:"AntreaPolicy"` @@ -207,8 +201,8 @@ func (r *AntreaConfigReconciler) ClusterToAntreaConfig(o client.Object) []ctrl.R return requests } -func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.AntreaConfig, client client.Client) (*antreaConfigSpec, error) { - configSpec := &antreaConfigSpec{} +func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.AntreaConfig, client client.Client) (*AntreaConfigSpec, error) { + configSpec := &AntreaConfigSpec{} // Derive InfraProvider from the cluster infraProvider, err := util.GetInfraProvider(cluster) diff --git a/addons/controllers/antreaconfig_controller_test.go b/addons/controllers/antreaconfig_controller_test.go index 302573d6b28..5505fe3786e 100644 --- a/addons/controllers/antreaconfig_controller_test.go +++ b/addons/controllers/antreaconfig_controller_test.go @@ -6,10 +6,10 @@ package controllers import ( "fmt" "os" - "strings" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "gopkg.in/yaml.v3" v1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -17,6 +17,7 @@ import ( clusterapiv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" + antreatype "github.com/vmware-tanzu/tanzu-framework/addons/controllers/antrea" cutil "github.com/vmware-tanzu/tanzu-framework/addons/controllers/utils" "github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants" "github.com/vmware-tanzu/tanzu-framework/addons/pkg/util" @@ -183,16 +184,19 @@ var _ = Describe("AntreaConfig Reconciler and Webhooks", func() { Expect(secret.Type).Should(Equal(v1.SecretTypeOpaque)) // check data value secret contents - secretData := string(secret.Data["values.yaml"]) - - Expect(strings.Contains(secretData, "serviceCIDR: 192.168.0.0/16")).Should(BeTrue()) - Expect(strings.Contains(secretData, "serviceCIDRv6: fd00:100:96::/48")).Should(BeTrue()) - Expect(strings.Contains(secretData, "infraProvider: docker")).Should(BeTrue()) - - Expect(strings.Contains(secretData, "trafficEncapMode: encap")).Should(BeTrue()) - Expect(strings.Contains(secretData, "tlsCipherSuites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384")).Should(BeTrue()) - Expect(strings.Contains(secretData, "AntreaProxy: true")).Should(BeTrue()) - Expect(strings.Contains(secretData, "AntreaPolicy: true")).Should(BeTrue()) + content := secret.Data["values.yaml"] + spec := antreatype.AntreaConfigSpec{} + err = yaml.Unmarshal(content, &spec) + if err != nil { + return false + } + Expect(spec.Antrea.AntreaConfigDataValue.ServiceCIDR, "192.168.0.0/16").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.ServiceCIDRv6, "fd00:100:96::/48").Should(BeTrue()) + Expect(spec.InfraProvider, "docker").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.TrafficEncapMode, "encap").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.TLSCipherSuites, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaProxy, true).Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaPolicy, true).Should(BeTrue()) return true }, waitTimeout, pollingInterval).Should(BeTrue()) @@ -302,16 +306,19 @@ var _ = Describe("AntreaConfig Reconciler and Webhooks", func() { Expect(secret.Type).Should(Equal(v1.SecretTypeOpaque)) // check data value secret contents - secretData := string(secret.Data["values.yaml"]) - - Expect(strings.Contains(secretData, "serviceCIDR: 192.168.0.0/16")).Should(BeTrue()) - Expect(strings.Contains(secretData, "serviceCIDRv6: fd00:100:96::/48")).Should(BeTrue()) - Expect(strings.Contains(secretData, "infraProvider: vsphere")).Should(BeTrue()) - - Expect(strings.Contains(secretData, "trafficEncapMode: encap")).Should(BeTrue()) - Expect(strings.Contains(secretData, "tlsCipherSuites: TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384")).Should(BeTrue()) - Expect(strings.Contains(secretData, "AntreaProxy: true")).Should(BeTrue()) - Expect(strings.Contains(secretData, "AntreaPolicy: true")).Should(BeTrue()) + content := secret.Data["values.yaml"] + spec := antreatype.AntreaConfigSpec{} + err = yaml.Unmarshal(content, &spec) + if err != nil { + return false + } + Expect(spec.Antrea.AntreaConfigDataValue.ServiceCIDR, "192.168.0.0/16").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.ServiceCIDRv6, "fd00:100:96::/48").Should(BeTrue()) + Expect(spec.InfraProvider, "vsphere").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.TrafficEncapMode, "encap").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.TLSCipherSuites, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384").Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaProxy, true).Should(BeTrue()) + Expect(spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaPolicy, true).Should(BeTrue()) return true }, waitTimeout, pollingInterval).Should(BeTrue())