istio.io/istio@v0.0.0-20240520182934-d79c90f27776/cni/pkg/repair/repair_test_helpers.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 repair
    16  
    17  import (
    18  	corev1 "k8s.io/api/core/v1"
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  
    21  	"istio.io/istio/tools/istio-iptables/pkg/constants"
    22  )
    23  
    24  type makePodArgs struct {
    25  	PodName             string
    26  	Labels              map[string]string
    27  	Annotations         map[string]string
    28  	InitContainerName   string
    29  	InitContainerStatus *corev1.ContainerStatus
    30  	NodeName            string
    31  }
    32  
    33  func makePod(args makePodArgs) *corev1.Pod {
    34  	pod := &corev1.Pod{
    35  		TypeMeta: metav1.TypeMeta{
    36  			Kind:       "Pod",
    37  			APIVersion: "v1",
    38  		},
    39  		ObjectMeta: metav1.ObjectMeta{
    40  			Name:        args.PodName,
    41  			Namespace:   "default",
    42  			Labels:      args.Labels,
    43  			Annotations: args.Annotations,
    44  		},
    45  		Spec: corev1.PodSpec{
    46  			NodeName: args.NodeName,
    47  			Volumes:  nil,
    48  			InitContainers: []corev1.Container{
    49  				{
    50  					Name: args.InitContainerName,
    51  				},
    52  			},
    53  			Containers: []corev1.Container{
    54  				{
    55  					Name: "payload-container",
    56  				},
    57  			},
    58  		},
    59  		Status: corev1.PodStatus{
    60  			InitContainerStatuses: []corev1.ContainerStatus{
    61  				*args.InitContainerStatus,
    62  			},
    63  			ContainerStatuses: []corev1.ContainerStatus{
    64  				{
    65  					Name: "payload-container",
    66  					State: corev1.ContainerState{
    67  						Waiting: &corev1.ContainerStateWaiting{
    68  							Reason: "PodInitializing",
    69  						},
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  	return pod
    76  }
    77  
    78  // Container specs
    79  var (
    80  	brokenInitContainerWaiting = corev1.ContainerStatus{
    81  		Name: constants.ValidationContainerName,
    82  		State: corev1.ContainerState{
    83  			Waiting: &corev1.ContainerStateWaiting{
    84  				Reason:  "CrashLoopBackOff",
    85  				Message: "Back-off 5m0s restarting failed blah blah blah",
    86  			},
    87  		},
    88  		LastTerminationState: corev1.ContainerState{
    89  			Terminated: &corev1.ContainerStateTerminated{
    90  				ExitCode: constants.ValidationErrorCode,
    91  				Reason:   "Error",
    92  				Message:  "Died for some reason",
    93  			},
    94  		},
    95  	}
    96  
    97  	brokenInitContainerTerminating = corev1.ContainerStatus{
    98  		Name: constants.ValidationContainerName,
    99  		State: corev1.ContainerState{
   100  			Terminated: &corev1.ContainerStateTerminated{
   101  				ExitCode: constants.ValidationErrorCode,
   102  				Reason:   "Error",
   103  				Message:  "Died for some reason",
   104  			},
   105  		},
   106  		LastTerminationState: corev1.ContainerState{
   107  			Terminated: &corev1.ContainerStateTerminated{
   108  				ExitCode: constants.ValidationErrorCode,
   109  				Reason:   "Error",
   110  				Message:  "Died for some reason",
   111  			},
   112  		},
   113  	}
   114  
   115  	workingInitContainerDiedPreviously = corev1.ContainerStatus{
   116  		Name: constants.ValidationContainerName,
   117  		State: corev1.ContainerState{
   118  			Terminated: &corev1.ContainerStateTerminated{
   119  				ExitCode: 0,
   120  				Reason:   "Completed",
   121  			},
   122  		},
   123  		LastTerminationState: corev1.ContainerState{
   124  			Terminated: &corev1.ContainerStateTerminated{
   125  				ExitCode: 126,
   126  				Reason:   "Error",
   127  				Message:  "Died for some reason",
   128  			},
   129  		},
   130  	}
   131  
   132  	workingInitContainer = corev1.ContainerStatus{
   133  		Name: constants.ValidationContainerName,
   134  		State: corev1.ContainerState{
   135  			Terminated: &corev1.ContainerStateTerminated{
   136  				ExitCode: 0,
   137  				Reason:   "Completed",
   138  			},
   139  		},
   140  	}
   141  )
   142  
   143  // Pod specs
   144  var (
   145  	brokenPodTerminating = makePod(makePodArgs{
   146  		PodName: "broken-pod-terminating",
   147  		Annotations: map[string]string{
   148  			"sidecar.istio.io/status": "something",
   149  		},
   150  		Labels: map[string]string{
   151  			"testlabel": "true",
   152  		},
   153  		NodeName:            "test-node",
   154  		InitContainerStatus: &brokenInitContainerTerminating,
   155  	})
   156  
   157  	brokenPodWaiting = makePod(makePodArgs{
   158  		PodName: "broken-pod-waiting",
   159  		Annotations: map[string]string{
   160  			"sidecar.istio.io/status": "something",
   161  		},
   162  		NodeName:            "test-node",
   163  		InitContainerStatus: &brokenInitContainerWaiting,
   164  	})
   165  
   166  	brokenPodNoAnnotation = makePod(makePodArgs{
   167  		PodName:             "broken-pod-no-annotation",
   168  		InitContainerStatus: &brokenInitContainerWaiting,
   169  	})
   170  
   171  	workingPod = makePod(makePodArgs{
   172  		PodName: "working-pod",
   173  		Annotations: map[string]string{
   174  			"sidecar.istio.io/status": "something",
   175  		},
   176  		InitContainerStatus: &workingInitContainer,
   177  	})
   178  
   179  	workingPodDiedPreviously = makePod(makePodArgs{
   180  		PodName: "working-pod-died-previously",
   181  		Annotations: map[string]string{
   182  			"sidecar.istio.io/status": "something",
   183  		},
   184  		InitContainerStatus: &workingInitContainerDiedPreviously,
   185  	})
   186  )