...

Source file src/github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/sidecar/misc.go

Documentation: github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/chaos/sidecar

     1  // Copyright 2021 Chaos Mesh 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  
    16  package sidecar
    17  
    18  import (
    19  	"context"
    20  	"strings"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/utils/exec"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/e2e-test/e2e/e2econst"
    29  )
    30  
    31  func createTemplateConfig(
    32  	ctx context.Context,
    33  	cli client.Client,
    34  	name string,
    35  	data map[string]string,
    36  ) error {
    37  	cm := &corev1.ConfigMap{
    38  		ObjectMeta: metav1.ObjectMeta{
    39  			Namespace: e2econst.ChaosMeshNamespace,
    40  			Name:      name,
    41  			Labels: map[string]string{
    42  				"app.kubernetes.io/component": "template",
    43  			},
    44  		},
    45  		Data: data,
    46  	}
    47  	return cli.Create(ctx, cm)
    48  }
    49  
    50  func createInjectionConfig(
    51  	ctx context.Context,
    52  	cli client.Client,
    53  	ns, name string,
    54  	data map[string]string,
    55  ) error {
    56  	cm := &corev1.ConfigMap{
    57  		ObjectMeta: metav1.ObjectMeta{
    58  			Namespace: ns,
    59  			Name:      name,
    60  			Labels: map[string]string{
    61  				"app.kubernetes.io/component": "webhook",
    62  			},
    63  		},
    64  		Data: data,
    65  	}
    66  	return cli.Create(ctx, cm)
    67  }
    68  
    69  // enableWebhook enables webhook on the specific namespace
    70  func enableWebhook(ns string) error {
    71  	args := []string{"label", "ns", ns, "--overwrite", "admission-webhook=enabled"}
    72  	out, err := exec.New().Command("kubectl", args...).CombinedOutput()
    73  	if err != nil {
    74  		klog.Fatalf("Failed to run 'kubectl %s'\nCombined output: %q\nError: %v", strings.Join(args, " "), string(out), err)
    75  	}
    76  	return nil
    77  }
    78