...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/utils/generate.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/utils

     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 utils
    17  
    18  import (
    19  	"fmt"
    20  
    21  	v1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/runtime"
    24  )
    25  
    26  // PodArg by default use `Status=corev1.PodRunning` and `Namespace=metav1.NamespaceDefault`.
    27  // For the others, the default values are empty.
    28  type PodArg struct {
    29  	Name            string
    30  	Status          v1.PodPhase
    31  	Namespace       string
    32  	Ans             map[string]string
    33  	Labels          map[string]string
    34  	ContainerStatus v1.ContainerStatus
    35  	Nodename        string
    36  }
    37  
    38  func newPod(p PodArg) v1.Pod {
    39  	if p.Status == "" {
    40  		p.Status = v1.PodRunning
    41  	}
    42  	if p.Namespace == "" {
    43  		p.Namespace = metav1.NamespaceDefault
    44  	}
    45  	return v1.Pod{
    46  		TypeMeta: metav1.TypeMeta{
    47  			Kind:       "Pod",
    48  			APIVersion: "v1",
    49  		},
    50  		ObjectMeta: metav1.ObjectMeta{
    51  			Name:        p.Name,
    52  			Namespace:   p.Namespace,
    53  			Labels:      p.Labels,
    54  			Annotations: p.Ans,
    55  		},
    56  		Spec: v1.PodSpec{
    57  			NodeName: p.Nodename,
    58  		},
    59  		Status: v1.PodStatus{
    60  			Phase:             p.Status,
    61  			ContainerStatuses: []v1.ContainerStatus{p.ContainerStatus},
    62  		},
    63  	}
    64  }
    65  
    66  func GenerateNPods(
    67  	namePrefix string,
    68  	n int,
    69  	podArg PodArg,
    70  ) ([]runtime.Object, []v1.Pod) {
    71  	var podObjects []runtime.Object
    72  	var pods []v1.Pod
    73  	for i := 0; i < n; i++ {
    74  		podArg.Name = fmt.Sprintf("%s%d", namePrefix, i)
    75  		pod := newPod(podArg)
    76  		podObjects = append(podObjects, &pod)
    77  		pods = append(pods, pod)
    78  	}
    79  
    80  	return podObjects, pods
    81  }
    82  
    83  func newNode(
    84  	name string,
    85  	label map[string]string,
    86  ) v1.Node {
    87  	return v1.Node{
    88  		TypeMeta: metav1.TypeMeta{
    89  			Kind:       "Node",
    90  			APIVersion: "v1",
    91  		},
    92  		ObjectMeta: metav1.ObjectMeta{
    93  			Name:   name,
    94  			Labels: label,
    95  		},
    96  	}
    97  }
    98  
    99  func GenerateNNodes(
   100  	namePrefix string,
   101  	n int,
   102  	label map[string]string,
   103  ) ([]runtime.Object, []v1.Node) {
   104  	var nodeObjects []runtime.Object
   105  	var nodes []v1.Node
   106  
   107  	for i := 0; i < n; i++ {
   108  		node := newNode(fmt.Sprintf("%s%d", namePrefix, i), label)
   109  		nodeObjects = append(nodeObjects, &node)
   110  		nodes = append(nodes, node)
   111  	}
   112  	return nodeObjects, nodes
   113  }
   114