创建k8s资源,并等待完成后在执行下一步

范姐 发表于: 2021-02-23   最后更新时间: 2021-04-03 13:43:43   1,640 游览

我需要创建一个k8s资源,这个资源需要一段时间才能使用,为此我使用了以下方法

op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deploy, func() error {

})

func2()

现在我需要在对象创建完成后立即调用func2(可能需要2-3分钟才能完成),我应该如何做?

发表于 2021-02-23
添加评论

下面是一个如何创建部署并检查它是否有至少一个准备好的副本的例子。也许检查状态中的条件会更好,并查找类型为Available和status为True的条件。

package main

import (
    "context"
    "fmt"
    v1 "k8s.io/api/apps/v1"
    podv1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/fields"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/tools/cache"
    "sigs.k8s.io/controller-runtime/pkg/client"
    "sigs.k8s.io/controller-runtime/pkg/client/config"
    "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
    "time"
)

const (
    namespace      = "default"
    deploymentName = "nginx"
)

func main() {
    cfg, err := config.GetConfig()
    if err != nil {
        panic(err)
    }

    clientset, err := kubernetes.NewForConfig(cfg)
    if err != nil {
        panic(err)
    }

    client, err := client.New(cfg, client.Options{})
    if err != nil {
        panic(err)
    }

    d := &v1.Deployment{
        ObjectMeta: metav1.ObjectMeta{
            Name:      deploymentName,
            Namespace: namespace,
        },
        Spec: v1.DeploymentSpec{
            Replicas: toInt32Ptr(2),
            Selector: &metav1.LabelSelector{
                MatchLabels: map[string]string{
                    "app": "nginx",
                },
            },
            Template: podv1.PodTemplateSpec{
                ObjectMeta: metav1.ObjectMeta{
                    Labels: map[string]string{
                        "app": "nginx",
                    },
                },
                Spec: podv1.PodSpec{
                    Containers: []podv1.Container{
                        {
                            Name:  "nginx",
                            Image: "nginx",
                        },
                    },
                },
            },
        },
    }

    fmt.Println("Deploying")
    _, err = controllerutil.CreateOrUpdate(context.TODO(), client, d, func() error {
        return nil
    })

    if err != nil {
        panic(err)
    }

    stop := make(chan struct{})

    watchList := cache.NewListWatchFromClient(clientset.AppsV1().RESTClient(), "deployments", namespace, fields.Everything())
    _, ctrl := cache.NewInformer(watchList, &v1.Deployment{}, time.Second, cache.ResourceEventHandlerFuncs{
        UpdateFunc: func(o, n interface{}) {
            newDeployment := n.(*v1.Deployment)
            if newDeployment.Name != deploymentName {
                return
            }

            if newDeployment.Status.ReadyReplicas > 0 {
                close(stop)
                return
            }

            return
        },
    })

    ctrl.Run(stop)
    fmt.Println("Deployment has at least 1 ready replica")
}

func toInt32Ptr(i int32) *int32 {
    return &i
}
你的答案

查看kubernetes相关的其他问题或提一个您自己的问题