Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch traceflow CRD validation to webhook validation. #5230

Merged
merged 1 commit into from
Aug 21, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: {{ .Release.Namespace }}
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
15 changes: 15 additions & 0 deletions build/yamls/antrea-aks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7424,3 +7424,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: kube-system
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
15 changes: 15 additions & 0 deletions build/yamls/antrea-eks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7425,3 +7425,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: kube-system
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
15 changes: 15 additions & 0 deletions build/yamls/antrea-gke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7422,3 +7422,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: kube-system
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
15 changes: 15 additions & 0 deletions build/yamls/antrea-ipsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7481,3 +7481,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: kube-system
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
15 changes: 15 additions & 0 deletions build/yamls/antrea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7422,3 +7422,18 @@ webhooks:
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
- name: "traceflowvalidator.antrea.io"
clientConfig:
service:
name: "antrea"
namespace: kube-system
path: "/validate/traceflow"
rules:
- operations: ["CREATE", "UPDATE"]
apiGroups: ["crd.antrea.io"]
apiVersions: ["v1alpha1"]
resources: ["traceflows"]
scope: "Cluster"
admissionReviewVersions: ["v1", "v1beta1"]
sideEffects: None
timeoutSeconds: 5
6 changes: 5 additions & 1 deletion cmd/antrea-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ var allowedPaths = []string{
"/validate/group",
"/validate/ippool",
"/validate/supportbundlecollection",
"/validate/traceflow",
"/convert/clustergroup",
}

Expand Down Expand Up @@ -298,6 +299,7 @@ func run(o *Options) error {
egressController,
statsAggregator,
bundleCollectionController,
traceflowController,
*o.config.EnablePrometheusMetrics,
cipherSuites,
cipher.TLSVersionMap[o.config.TLSMinVersion])
Expand Down Expand Up @@ -490,6 +492,7 @@ func createAPIServerConfig(kubeconfig string,
egressController *egress.EgressController,
statsAggregator *stats.Aggregator,
bundleCollectionStore *supportbundlecollection.Controller,
traceflowController *traceflow.Controller,
enableMetrics bool,
cipherSuites []uint16,
tlsMinVersion uint16) (*apiserver.Config, error) {
Expand Down Expand Up @@ -556,5 +559,6 @@ func createAPIServerConfig(kubeconfig string,
endpointQuerier,
npController,
egressController,
bundleCollectionStore), nil
bundleCollectionStore,
traceflowController), nil
}
14 changes: 1 addition & 13 deletions pkg/agent/controller/traceflow/traceflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,6 @@ func (c *Controller) startTraceflow(tf *crdv1beta1.Traceflow) error {
return err
}

liveTraffic := tf.Spec.LiveTraffic
if tf.Spec.Source.Pod == "" && tf.Spec.Destination.Pod == "" {
klog.Errorf("Traceflow %s has neither source nor destination Pod specified", tf.Name)
return nil
}
if tf.Spec.Source.Pod == "" && !liveTraffic {
klog.Errorf("Traceflow %s does not have source Pod specified", tf.Name)
return nil
}

receiverOnly := false
var pod, ns string
if tf.Spec.Source.Pod != "" {
Expand All @@ -327,6 +317,7 @@ func (c *Controller) startTraceflow(tf *crdv1beta1.Traceflow) error {
podInterfaces := c.interfaceStore.GetContainerInterfacesByPod(pod, ns)
isSender := len(podInterfaces) > 0 && !receiverOnly

liveTraffic := tf.Spec.LiveTraffic
var packet, matchPacket *binding.Packet
var ofPort uint32
if len(podInterfaces) > 0 {
Expand Down Expand Up @@ -388,9 +379,6 @@ func (c *Controller) validateTraceflow(tf *crdv1beta1.Traceflow) error {
}
if tf.Spec.Destination.IP != "" {
destIP := net.ParseIP(tf.Spec.Destination.IP)
if destIP == nil {
return fmt.Errorf("destination IP is not valid: %s", tf.Spec.Destination.IP)
}
// When AntreaProxy is enabled, serviceCIDR is not required and may be set to a
// default value which does not match the cluster configuration.
if !features.DefaultFeatureGate.Enabled(features.AntreaProxy) && c.serviceCIDR.Contains(destIP) {
Expand Down
55 changes: 0 additions & 55 deletions pkg/agent/controller/traceflow/traceflow_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,49 +622,6 @@ func TestStartTraceflow(t *testing.T) {
}, ofPortPod1, int32(-1))
},
},
{
name: "empty source and destination Pod",
tf: &crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf3", UID: "uid3"},
},
expectedErrLog: "Traceflow tf3 has neither source nor destination Pod specified",
},
{
name: "empty source Pod",
tf: &crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf4", UID: "uid4"},
Spec: crdv1beta1.TraceflowSpec{
Destination: crdv1beta1.Destination{
Namespace: pod2.Namespace,
Pod: pod2.Name,
},
},
},
expectedErrLog: "Traceflow tf4 does not have source Pod specified",
},
{
name: "invalid destination IPv4",
tf: &crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf5", UID: "uid5"},
Spec: crdv1beta1.TraceflowSpec{
Source: crdv1beta1.Source{
Namespace: pod1.Namespace,
Pod: pod1.Name,
},
Destination: crdv1beta1.Destination{
IP: "192.168.1.300",
},
},
Status: crdv1beta1.TraceflowStatus{
Phase: crdv1beta1.Running,
DataplaneTag: 1,
},
},
nodeConfig: &config.NodeConfig{
Name: "node-1",
},
expectedErr: "destination IP is not valid: 192.168.1.300",
},
{
name: "live traceflow receive only",
tf: &crdv1beta1.Traceflow{
Expand Down Expand Up @@ -864,18 +821,6 @@ func TestValidateTraceflow(t *testing.T) {
},
expectedErr: "using Service destination requires AntreaProxy feature enabled",
},
{
name: "invalid destination IPv4",
tf: &crdv1beta1.Traceflow{
Spec: crdv1beta1.TraceflowSpec{
Destination: crdv1beta1.Destination{
IP: "192.168.1.300",
},
},
},
antreaProxyEnabled: true,
expectedErr: "destination IP is not valid: 192.168.1.300",
},
{
name: "AntreaProxy feature disabled with ClusterIP destination",
tf: &crdv1beta1.Traceflow{
Expand Down
10 changes: 9 additions & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import (
"antrea.io/antrea/pkg/controller/querier"
"antrea.io/antrea/pkg/controller/stats"
controllerbundlecollection "antrea.io/antrea/pkg/controller/supportbundlecollection"
"antrea.io/antrea/pkg/controller/traceflow"
"antrea.io/antrea/pkg/features"
)

Expand Down Expand Up @@ -116,6 +117,7 @@ type ExtraConfig struct {
statsAggregator *stats.Aggregator
networkPolicyStatusController *controllernetworkpolicy.StatusController
bundleCollectionController *controllerbundlecollection.Controller
traceflowController *traceflow.Controller
}

// Config defines the config for Antrea apiserver.
Expand Down Expand Up @@ -158,7 +160,8 @@ func NewConfig(
endpointQuerier controllernetworkpolicy.EndpointQuerier,
npController *controllernetworkpolicy.NetworkPolicyController,
egressController *egress.EgressController,
bundleCollectionController *controllerbundlecollection.Controller) *Config {
bundleCollectionController *controllerbundlecollection.Controller,
traceflowController *traceflow.Controller) *Config {
return &Config{
genericConfig: genericConfig,
extraConfig: ExtraConfig{
Expand All @@ -178,6 +181,7 @@ func NewConfig(
networkPolicyStatusController: networkPolicyStatusController,
egressController: egressController,
bundleCollectionController: bundleCollectionController,
traceflowController: traceflowController,
},
}
}
Expand Down Expand Up @@ -334,6 +338,10 @@ func installHandlers(c *ExtraConfig, s *genericapiserver.GenericAPIServer) {
if features.DefaultFeatureGate.Enabled(features.SupportBundleCollection) {
s.Handler.NonGoRestfulMux.HandleFunc("/validate/supportbundlecollection", webhook.HandlerForValidateFunc(c.bundleCollectionController.Validate))
}

if features.DefaultFeatureGate.Enabled(features.Traceflow) {
s.Handler.NonGoRestfulMux.HandleFunc("/validate/traceflow", webhook.HandlerForValidateFunc(c.traceflowController.Validate))
shi0rik0 marked this conversation as resolved.
Show resolved Hide resolved
}
}

func DefaultCAConfig() *certificate.CAConfig {
Expand Down
21 changes: 0 additions & 21 deletions pkg/controller/traceflow/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
crdinformers "antrea.io/antrea/pkg/client/informers/externalversions/crd/v1beta1"
crdlisters "antrea.io/antrea/pkg/client/listers/crd/v1beta1"
"antrea.io/antrea/pkg/controller/grouping"
"antrea.io/antrea/pkg/util/k8s"
)

const (
Expand Down Expand Up @@ -249,10 +248,6 @@ func (c *Controller) syncTraceflow(traceflowName string) error {
}

func (c *Controller) startTraceflow(tf *crdv1beta1.Traceflow) error {
if err := c.validateTraceflow(tf); err != nil {
klog.ErrorS(err, "Invalid Traceflow request", "request", tf)
return c.updateTraceflowStatus(tf, crdv1beta1.Failed, fmt.Sprintf("Invalid Traceflow request, err: %+v", err), 0)
}
// Allocate data plane tag.
tag, err := c.allocateTag(tf.Name)
if err != nil {
Expand Down Expand Up @@ -413,19 +408,3 @@ func (c *Controller) deallocateTag(name string, tag uint8) {
}
}
}

func (c *Controller) validateTraceflow(tf *crdv1beta1.Traceflow) error {
if !tf.Spec.LiveTraffic {
srcPod, err := c.podLister.Pods(tf.Spec.Source.Namespace).Get(tf.Spec.Source.Pod)
if err != nil {
if apierrors.IsNotFound(err) {
err = fmt.Errorf("requested source Pod %s not found", k8s.NamespacedName(tf.Spec.Source.Namespace, tf.Spec.Source.Pod))
}
return err
}
if srcPod.Spec.HostNetwork {
return fmt.Errorf("using hostNetwork Pod as source in non-live-traffic Traceflow is not supported")
}
}
return nil
}
28 changes: 0 additions & 28 deletions pkg/controller/traceflow/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,34 +147,6 @@ func TestTraceflow(t *testing.T) {
assert.Equal(t, numRunningTraceflows(), 0)
})

t.Run("timeoutHostnetworkTraceflow", func(t *testing.T) {
tf2 := crdv1beta1.Traceflow{
ObjectMeta: metav1.ObjectMeta{Name: "tf2", UID: "uid2"},
Spec: crdv1beta1.TraceflowSpec{
Source: crdv1beta1.Source{Namespace: "ns1", Pod: "pod2"},
Destination: crdv1beta1.Destination{Namespace: "ns2", Pod: "pod2"},
Timeout: 2, // 2 seconds timeout
},
}
pod2 := corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod2",
Namespace: "ns1",
},
Spec: corev1.PodSpec{HostNetwork: true},
}

tfc.kubeClient.CoreV1().Pods("ns1").Create(context.TODO(), &pod2, metav1.CreateOptions{})
createdPod, _ := tfc.waitForPodInNamespace("ns1", "pod2", time.Second)
require.NotNil(t, createdPod)
tfc.client.CrdV1beta1().Traceflows().Create(context.TODO(), &tf2, metav1.CreateOptions{})
res, _ := tfc.waitForTraceflow("tf2", crdv1beta1.Failed, time.Second)
require.NotNil(t, res)
// DataplaneTag should not be allocated by Controller.
assert.True(t, res.Status.DataplaneTag == 0)
assert.Equal(t, numRunningTraceflows(), 0)
})

close(stopCh)
}

Expand Down
Loading