You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

namespace.go 1.6 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package huawei
  2. import (
  3. "time"
  4. corev1 "k8s.io/api/core/v1"
  5. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  6. "k8s.io/apimachinery/pkg/util/wait"
  7. "k8s.io/client-go/kubernetes"
  8. )
  9. const (
  10. namespace = "test-k8s-client-namespace"
  11. )
  12. // CreateNamespace 创建命名空间
  13. // API参考:https://support.huaweicloud.com/api-cci/createCoreV1Namespace.html
  14. func CreateNamespace(cs *kubernetes.Clientset) (*corev1.Namespace, error) {
  15. namespace := &corev1.Namespace{
  16. TypeMeta: metav1.TypeMeta{},
  17. ObjectMeta: metav1.ObjectMeta{
  18. Name: namespace,
  19. Annotations: map[string]string{
  20. "namespace.kubernetes.io/flavor": "general-computing",
  21. "network.cci.io/warm-pool-size": "10",
  22. },
  23. Labels: map[string]string{
  24. "rbac.authorization.cci.io/enable-k8s-rbac": "false",
  25. },
  26. },
  27. }
  28. return cs.CoreV1().Namespaces().Create(namespace)
  29. }
  30. // DeleteNamespace 删除Namespace
  31. // API参考:https://support.huaweicloud.com/api-cci/deleteCoreV1Namespace.html
  32. func DeleteNamespace(cs *kubernetes.Clientset) error {
  33. return cs.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{})
  34. }
  35. // WaitNamespaceActive 查询Namespace状态,等待其状态变为"Active"
  36. // API参考:https://support.huaweicloud.com/api-cci/readCoreV1Namespace.html
  37. func WaitNamespaceActive(cs *kubernetes.Clientset) error {
  38. return wait.Poll(time.Second*5, time.Second*30, func() (done bool, err error) {
  39. ns, err := cs.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{})
  40. if err != nil {
  41. return false, err
  42. }
  43. if ns.Status.Phase == corev1.NamespaceActive {
  44. return true, nil
  45. }
  46. return false, nil
  47. })
  48. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.