...

Source file src/k8s.io/kubernetes/cmd/kubeadm/test/resources/pods.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/test/resources

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resources
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	v1 "k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	clientset "k8s.io/client-go/kubernetes"
    26  
    27  	staticpodutil "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod"
    28  )
    29  
    30  // FakeStaticPod represents a fake static pod
    31  type FakeStaticPod struct {
    32  	NodeName    string
    33  	Component   string
    34  	Annotations map[string]string
    35  }
    36  
    37  // Pod returns a pod structure representing the fake static pod with a
    38  // given suffix
    39  func (p *FakeStaticPod) Pod(suffix string) *v1.Pod {
    40  	pod := staticpodutil.ComponentPod(
    41  		v1.Container{
    42  			Name:  p.Component,
    43  			Image: fmt.Sprintf("%s-image:tag", p.Component),
    44  		},
    45  		map[string]v1.Volume{},
    46  		p.Annotations,
    47  	)
    48  	if len(suffix) > 0 {
    49  		pod.ObjectMeta.Name = fmt.Sprintf("%s-%s-%s", p.Component, p.NodeName, suffix)
    50  	} else {
    51  		pod.ObjectMeta.Name = fmt.Sprintf("%s-%s", p.Component, p.NodeName)
    52  	}
    53  	pod.Spec.NodeName = p.NodeName
    54  	return &pod
    55  }
    56  
    57  // Create creates a fake static pod using the provided client
    58  func (p *FakeStaticPod) Create(client clientset.Interface) error {
    59  	return p.CreateWithPodSuffix(client, "")
    60  }
    61  
    62  // CreateWithPodSuffix creates a fake static pod using the provided
    63  // client and suffix
    64  func (p *FakeStaticPod) CreateWithPodSuffix(client clientset.Interface, suffix string) error {
    65  	_, err := client.CoreV1().Pods(metav1.NamespaceSystem).Create(context.TODO(), p.Pod(suffix), metav1.CreateOptions{})
    66  	return err
    67  }
    68  

View as plain text