Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Add version check for antreaconfig fields (#4336)
Browse files Browse the repository at this point in the history
Add a version switch to make the latest fields from antrea 1.7.1+
invisible to old antrea versions.

Signed-off-by: Hang Yan <[email protected]>
  • Loading branch information
hangyan authored Feb 13, 2023
1 parent cd7c089 commit a4f9eff
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 27 deletions.
10 changes: 10 additions & 0 deletions addons/controllers/antrea/antreaconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"context"
"fmt"

"github.com/pkg/errors"

"github.com/go-logr/logr"
yaml "gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
Expand All @@ -24,6 +26,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"

addontypes "github.com/vmware-tanzu/tanzu-framework/addons/pkg/types"

cutil "github.com/vmware-tanzu/tanzu-framework/addons/controllers/utils"
addonconfig "github.com/vmware-tanzu/tanzu-framework/addons/pkg/config"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants"
Expand Down Expand Up @@ -71,6 +75,12 @@ func (r *AntreaConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request
return ctrl.Result{}, nil
}

labels := antreaConfig.GetLabels()
if _, ok := labels[addontypes.PackageNameLabel]; !ok {
r.Log.Info(fmt.Sprintf("AntreaConfig resource '%v' does not contains package name label", req.NamespacedName))
return ctrl.Result{}, errors.New("AntreaConfig does not contains package name label")
}

// deep copy AntreaConfig to avoid issues if in the future other controllers where interacting with the same copy
antreaConfig = antreaConfig.DeepCopy()

Expand Down
63 changes: 40 additions & 23 deletions addons/controllers/antrea/antreaconfig_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ package controllers
import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"

"golang.org/x/mod/semver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1beta1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterapiutil "sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/vmware-tanzu/tanzu-framework/addons/pkg/constants"
addontypes "github.com/vmware-tanzu/tanzu-framework/addons/pkg/types"
"github.com/vmware-tanzu/tanzu-framework/addons/pkg/util"
cniv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/addonconfigs/cni/v1alpha1"
)
Expand Down Expand Up @@ -94,20 +98,20 @@ type antreaConfigDataValue struct {
}

type antreaFeatureGates struct {
AntreaProxy bool `yaml:"AntreaProxy"`
EndpointSlice bool `yaml:"EndpointSlice"`
AntreaPolicy bool `yaml:"AntreaPolicy"`
FlowExporter bool `yaml:"FlowExporter"`
Egress bool `yaml:"Egress"`
NodePortLocal bool `yaml:"NodePortLocal"`
AntreaTraceflow bool `yaml:"AntreaTraceflow"`
NetworkPolicyStats bool `yaml:"NetworkPolicyStats"`
AntreaIPAM bool `yaml:"AntreaIPAM"`
ServiceExternalIP bool `yaml:"ServiceExternalIP"`
Multicast bool `yaml:"Multicast"`
MultiCluster bool `yaml:"Multicluster"`
SecondaryNetwork bool `yaml:"SecondaryNetwork"`
TrafficControl bool `yaml:"TrafficControl"`
AntreaProxy bool `yaml:"AntreaProxy"`
EndpointSlice bool `yaml:"EndpointSlice"`
AntreaPolicy bool `yaml:"AntreaPolicy"`
FlowExporter bool `yaml:"FlowExporter"`
Egress bool `yaml:"Egress"`
NodePortLocal bool `yaml:"NodePortLocal"`
AntreaTraceflow bool `yaml:"AntreaTraceflow"`
NetworkPolicyStats bool `yaml:"NetworkPolicyStats"`
AntreaIPAM bool `yaml:"AntreaIPAM"`
ServiceExternalIP bool `yaml:"ServiceExternalIP"`
Multicast bool `yaml:"Multicast"`
MultiCluster *bool `yaml:"Multicluster,omitempty"`
SecondaryNetwork *bool `yaml:"SecondaryNetwork,omitempty"`
TrafficControl *bool `yaml:"TrafficControl,omitempty"`
}

// ClusterToAntreaConfig returns a list of Requests with AntreaConfig ObjectKey
Expand Down Expand Up @@ -162,6 +166,11 @@ func (r *AntreaConfigReconciler) ClusterToAntreaConfig(o client.Object) []ctrl.R
}

func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.AntreaConfig) (*antreaConfigSpec, error) {

packageName := config.GetLabels()[addontypes.PackageNameLabel]
version := strings.TrimPrefix(strings.Split(packageName, "---")[0], "antrea.tanzu.vmware.com.")
version = "v" + version

configSpec := &antreaConfigSpec{}

// Derive InfraProvider from the cluster
Expand Down Expand Up @@ -193,9 +202,7 @@ func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.An
configSpec.Antrea.AntreaConfigDataValue.FlowExporter.PollInterval = config.Spec.Antrea.AntreaConfigDataValue.AntreaFlowExporter.PollInterval
configSpec.Antrea.AntreaConfigDataValue.FlowExporter.ActiveFlowTimeout = config.Spec.Antrea.AntreaConfigDataValue.AntreaFlowExporter.ActiveFlowTimeout
configSpec.Antrea.AntreaConfigDataValue.FlowExporter.IdleFlowTimeout = config.Spec.Antrea.AntreaConfigDataValue.AntreaFlowExporter.IdleFlowTimeout
configSpec.Antrea.AntreaConfigDataValue.Multicast.IGMPQueryInterval = config.Spec.Antrea.AntreaConfigDataValue.Multicast.IGMPQueryInterval
configSpec.Antrea.AntreaConfigDataValue.MultiCluster.Enable = config.Spec.Antrea.AntreaConfigDataValue.MultiCluster.Enable
configSpec.Antrea.AntreaConfigDataValue.MultiCluster.Namespace = config.Spec.Antrea.AntreaConfigDataValue.MultiCluster.Namespace

configSpec.Antrea.AntreaConfigDataValue.KubeAPIServerOverride = config.Spec.Antrea.AntreaConfigDataValue.KubeAPIServerOverride
configSpec.Antrea.AntreaConfigDataValue.transportInterface = config.Spec.Antrea.AntreaConfigDataValue.TransportInterface
configSpec.Antrea.AntreaConfigDataValue.transportInterfaceCIDRs = config.Spec.Antrea.AntreaConfigDataValue.TransportInterfaceCIDRs
Expand All @@ -208,9 +215,15 @@ func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.An
configSpec.Antrea.AntreaConfigDataValue.TLSCipherSuites = config.Spec.Antrea.AntreaConfigDataValue.TLSCipherSuites
configSpec.Antrea.AntreaConfigDataValue.DisableUDPTunnelOffload = config.Spec.Antrea.AntreaConfigDataValue.DisableUDPTunnelOffload
configSpec.Antrea.AntreaConfigDataValue.DefaultMTU = config.Spec.Antrea.AntreaConfigDataValue.DefaultMTU
configSpec.Antrea.AntreaConfigDataValue.DNSServerOverride = config.Spec.Antrea.AntreaConfigDataValue.DNSServerOverride
configSpec.Antrea.AntreaConfigDataValue.DisableTXChecksumOffload = config.Spec.Antrea.AntreaConfigDataValue.DisableTXChecksumOffload
configSpec.Antrea.AntreaConfigDataValue.EnableBridgingMode = config.Spec.Antrea.AntreaConfigDataValue.EnableBridgingMode

if semver.Compare(version, "v1.7.1") >= 0 {
configSpec.Antrea.AntreaConfigDataValue.MultiCluster.Enable = config.Spec.Antrea.AntreaConfigDataValue.MultiCluster.Enable
configSpec.Antrea.AntreaConfigDataValue.MultiCluster.Namespace = config.Spec.Antrea.AntreaConfigDataValue.MultiCluster.Namespace
configSpec.Antrea.AntreaConfigDataValue.EnableBridgingMode = config.Spec.Antrea.AntreaConfigDataValue.EnableBridgingMode
configSpec.Antrea.AntreaConfigDataValue.DisableTXChecksumOffload = config.Spec.Antrea.AntreaConfigDataValue.DisableTXChecksumOffload
configSpec.Antrea.AntreaConfigDataValue.DNSServerOverride = config.Spec.Antrea.AntreaConfigDataValue.DNSServerOverride
configSpec.Antrea.AntreaConfigDataValue.Multicast.IGMPQueryInterval = config.Spec.Antrea.AntreaConfigDataValue.Multicast.IGMPQueryInterval
}

// FeatureGates
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaProxy = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaProxy
Expand All @@ -224,8 +237,12 @@ func mapAntreaConfigSpec(cluster *clusterv1beta1.Cluster, config *cniv1alpha1.An
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaIPAM = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.AntreaIPAM
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.ServiceExternalIP = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.ServiceExternalIP
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.Multicast = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.Multicast
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.MultiCluster = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.MultiCluster
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.SecondaryNetwork = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.SecondaryNetwork
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.TrafficControl = config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.TrafficControl

if semver.Compare(version, "v1.7.1") >= 0 {
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.SecondaryNetwork = &config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.SecondaryNetwork
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.TrafficControl = &config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.TrafficControl
configSpec.Antrea.AntreaConfigDataValue.FeatureGates.MultiCluster = &config.Spec.Antrea.AntreaConfigDataValue.FeatureGates.MultiCluster
}

return configSpec, nil
}
9 changes: 6 additions & 3 deletions addons/controllers/antreaconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ var _ = Describe("AntreaConfig Reconciler and Webhooks", func() {

By("Create antrea config in the cluster's namespace with expected name pattern", func() {
datavalues := &cniv1alpha1.AntreaConfigDataValue{DefaultMTU: newDefaultMTU}
antreaConfig := genearateAntreaConfig(configName, clusterNamespace, datavalues)
antreaConfig := generateAntreaConfig(configName, clusterNamespace, datavalues)
err := k8sClient.Create(ctx, antreaConfig)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(antreaConfig), antreaConfig)
Expand Down Expand Up @@ -361,7 +361,7 @@ var _ = Describe("AntreaConfig Reconciler and Webhooks", func() {

By("Create antrea config in the cluster's namespace with random name", func() {
datavalues := &cniv1alpha1.AntreaConfigDataValue{DefaultMTU: newDefaultMTU}
antreaConfig := genearateAntreaConfig(configName, clusterNamespace, datavalues)
antreaConfig := generateAntreaConfig(configName, clusterNamespace, datavalues)
err := k8sClient.Create(ctx, antreaConfig)
Expect(err).ToNot(HaveOccurred())
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(antreaConfig), antreaConfig)
Expand Down Expand Up @@ -415,11 +415,14 @@ var _ = Describe("AntreaConfig Reconciler and Webhooks", func() {
})
})

func genearateAntreaConfig(name, namespace string, datavalues *cniv1alpha1.AntreaConfigDataValue) *cniv1alpha1.AntreaConfig {
func generateAntreaConfig(name, namespace string, datavalues *cniv1alpha1.AntreaConfigDataValue) *cniv1alpha1.AntreaConfig {
labels := map[string]string{}
labels["tkg.tanzu.vmware.com/package-name"] = "antrea.tanzu.vmware.com.1.7.2---tkg.1-advanced"
config := &cniv1alpha1.AntreaConfig{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: cniv1alpha1.AntreaConfigSpec{
Antrea: cniv1alpha1.Antrea{
Expand Down
2 changes: 2 additions & 0 deletions addons/controllers/testdata/antrea-test-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ kind: AntreaConfig
metadata:
name: test-cluster-4-antrea-package
namespace: default
labels:
tkg.tanzu.vmware.com/package-name: antrea.tanzu.vmware.com.1.7.2---tkg.1-advanced
ownerReferences:
- apiVersion: cluster.x-k8s.io/v1beta1
blockOwnerDeletion: true
Expand Down
1 change: 1 addition & 0 deletions addons/controllers/testdata/test-cluster-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ metadata:
labels:
tkg.tanzu.vmware.com/addon-name: antrea
tkg.tanzu.vmware.com/cluster-name: test-cluster-1
tkg.tanzu.vmware.com/package-name: antrea.tanzu.vmware.com.1.7.2---tkg.1-advanced
# annotations:
# tkg.tanzu.vmware.com/remote-app: "true"
type: tkg.tanzu.vmware.com/addon
Expand Down
1 change: 1 addition & 0 deletions addons/controllers/testdata/test-cluster-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ metadata:
labels:
tkg.tanzu.vmware.com/addon-name: antrea
tkg.tanzu.vmware.com/cluster-name: test-cluster-2
tkg.tanzu.vmware.com/package-name: antrea.tanzu.vmware.com.1.7.2---tkg.1-advanced
# annotations:
# tkg.tanzu.vmware.com/remote-app: "true"
type: tkg.tanzu.vmware.com/addon
Expand Down
1 change: 1 addition & 0 deletions addons/controllers/testdata/test-cluster-3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ metadata:
labels:
tkg.tanzu.vmware.com/addon-name: antrea
tkg.tanzu.vmware.com/cluster-name: test-cluster-3
tkg.tanzu.vmware.com/package-name: antrea.tanzu.vmware.com.1.7.2---tkg.1-advanced
# annotations:
# tkg.tanzu.vmware.com/remote-app: "true"
type: tkg.tanzu.vmware.com/addon
Expand Down
2 changes: 1 addition & 1 deletion addons/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/vmware-tanzu/tanzu-framework/apis/run v0.0.0-20220907220230-c1137d344dd3
github.com/vmware-tanzu/vm-operator-api v0.1.4-0.20211202185235-43eb44c09ecd
github.com/vmware-tanzu/vm-operator/external/tanzu-topology v0.0.0-20211209213435-0f4ab286f64f
golang.org/x/mod v0.7.0
golang.org/x/text v0.5.0
golang.org/x/tools v0.1.12
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -102,7 +103,6 @@ require (
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.22.0 // indirect
golang.org/x/crypto v0.4.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/oauth2 v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
Expand Down

0 comments on commit a4f9eff

Please sign in to comment.