...

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

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

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