istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/kube_ingress_fuzzer.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fuzz
    16  
    17  import (
    18  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    19  	corev1 "k8s.io/api/core/v1"
    20  	knetworking "k8s.io/api/networking/v1"
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  
    23  	meshconfig "istio.io/api/mesh/v1alpha1"
    24  	"istio.io/istio/pilot/pkg/config/kube/ingress"
    25  	"istio.io/istio/pkg/config"
    26  	"istio.io/istio/pkg/kube"
    27  	"istio.io/istio/pkg/kube/kclient"
    28  )
    29  
    30  func FuzzConvertIngressVirtualService(data []byte) int {
    31  	f := fuzz.NewConsumer(data)
    32  	ing := knetworking.Ingress{}
    33  	err := f.GenerateStruct(&ing)
    34  	if err != nil {
    35  		return 0
    36  	}
    37  	service := &corev1.Service{}
    38  	cfgs := map[string]*config.Config{}
    39  	serviceLister, teardown := newServiceLister(service)
    40  	defer teardown()
    41  	ingress.ConvertIngressVirtualService(ing, "mydomain", cfgs, serviceLister)
    42  	return 1
    43  }
    44  
    45  func FuzzConvertIngressV1alpha3(data []byte) int {
    46  	f := fuzz.NewConsumer(data)
    47  	ing := knetworking.Ingress{}
    48  	err := f.GenerateStruct(&ing)
    49  	if err != nil {
    50  		return 0
    51  	}
    52  	m := &meshconfig.MeshConfig{}
    53  	err = f.GenerateStruct(m)
    54  	if err != nil {
    55  		return 0
    56  	}
    57  	ingress.ConvertIngressV1alpha3(ing, m, "mydomain")
    58  	return 1
    59  }
    60  
    61  func newServiceLister(objects ...runtime.Object) (kclient.Client[*corev1.Service], func()) {
    62  	kc := kube.NewFakeClient(objects...)
    63  	stop := make(chan struct{})
    64  	kc.RunAndWait(stop)
    65  	teardown := func() {
    66  		close(stop)
    67  	}
    68  	return kclient.New[*corev1.Service](kc), teardown
    69  }