pods.go 1.9 KB

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