...

Source file src/github.com/linkerd/linkerd2/testutil/inject.go

Documentation: github.com/linkerd/linkerd2/testutil

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	jsonpatch "github.com/evanphx/json-patch"
     8  	"github.com/linkerd/linkerd2/pkg/k8s"
     9  	v1 "k8s.io/api/core/v1"
    10  	"sigs.k8s.io/yaml"
    11  )
    12  
    13  func applyPatch(in string, patchJSON []byte) (string, error) {
    14  	patch, err := jsonpatch.DecodePatch(patchJSON)
    15  	if err != nil {
    16  		return "", err
    17  	}
    18  	json, err := yaml.YAMLToJSON([]byte(in))
    19  	if err != nil {
    20  		return "", err
    21  	}
    22  	patched, err := patch.Apply(json)
    23  	if err != nil {
    24  		return "", err
    25  	}
    26  	return string(patched), nil
    27  }
    28  
    29  // PatchDeploy patches a manifest by applying annotations
    30  func PatchDeploy(in string, name string, annotations map[string]string) (string, error) {
    31  	ops := []string{
    32  		fmt.Sprintf(`{"op": "replace", "path": "/metadata/name", "value": "%s"}`, name),
    33  		fmt.Sprintf(`{"op": "replace", "path": "/spec/selector/matchLabels/app", "value": "%s"}`, name),
    34  		fmt.Sprintf(`{"op": "replace", "path": "/spec/template/metadata/labels/app", "value": "%s"}`, name),
    35  	}
    36  
    37  	if len(annotations) > 0 {
    38  		ops = append(ops, `{"op": "add", "path": "/spec/template/metadata/annotations", "value": {}}`)
    39  		for k, v := range annotations {
    40  			ops = append(ops,
    41  				fmt.Sprintf(`{"op": "add", "path": "/spec/template/metadata/annotations/%s", "value": "%s"}`, strings.ReplaceAll(k, "/", "~1"), v),
    42  			)
    43  		}
    44  	}
    45  
    46  	patchJSON := []byte(fmt.Sprintf("[%s]", strings.Join(ops, ",")))
    47  
    48  	return applyPatch(in, patchJSON)
    49  }
    50  
    51  // GetProxyContainer get the proxy containers
    52  func GetProxyContainer(containers []v1.Container) *v1.Container {
    53  	for _, c := range containers {
    54  		container := c
    55  		if container.Name == k8s.ProxyContainerName {
    56  			return &container
    57  		}
    58  	}
    59  
    60  	return nil
    61  }
    62  

View as plain text