zoukankan      html  css  js  c++  java
  • kubebuilder开发一:从controller创建deployment

    1、直接在controller目录下创建文件virtualmachine_controller.go文件

    /*
    Copyright 2020 fanux.
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */
    
    package controllers
    
    import (
    	"context"
    	"fmt"
    
    	infrav1 "VM/api/v1"
    	"github.com/go-logr/logr"
    	appv1 "k8s.io/api/apps/v1"
    	corev1 "k8s.io/api/core/v1"
    	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    	"k8s.io/apimachinery/pkg/runtime"
    	ctrl "sigs.k8s.io/controller-runtime"
    	"sigs.k8s.io/controller-runtime/pkg/client"
    )
    
    // VirtulMachineReconciler reconciles a VirtulMachine object
    type VirtulMachineReconciler struct {
    	client.Client
    	Log    logr.Logger
    	Scheme *runtime.Scheme
    }
    
    // +kubebuilder:rbac:groups=infra.sealyun.com,resources=virtulmachines,verbs=get;list;watch;create;update;patch;delete
    // +kubebuilder:rbac:groups=infra.sealyun.com,resources=virtulmachines/status,verbs=get;update;patch
    
    func (r *VirtulMachineReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    	ctx := context.Background()
    	log := r.Log.WithValues("virtulmachine", req.NamespacedName)
    
    	// your logic here
    	vm := &infrav1.VirtulMachine{}
    	if err := r.Get(ctx, req.NamespacedName, vm); err != nil {
    		log.Error(err, "unable to fetch vm")
    	} else {
    		fmt.Println(vm.Spec.CPU, vm.Spec.Memory)
    	}
    	log.Info("get it ")
    	podLabels := map[string]string{
    		"app": req.Name,
    	}
    	// 拼凑deploy资源的定义
    	deployment := appv1.Deployment{
    		TypeMeta: metav1.TypeMeta{
    			Kind:       "Deployment",
    			APIVersion: "apps/v1",
    		},
    		ObjectMeta: metav1.ObjectMeta{
    			Name:      req.Name,
    			Namespace: req.Namespace,
    		},
    		Spec: appv1.DeploymentSpec{
    			Selector: &metav1.LabelSelector{
    				MatchLabels: podLabels,
    			},
    			Template: corev1.PodTemplateSpec{
    				ObjectMeta: metav1.ObjectMeta{
    					Labels: podLabels,
    				},
    				Spec: corev1.PodSpec{
    					Containers: []corev1.Container{
    						{
    							Name: req.Name,
    							Image: "nginx:1.13.5-alpine",
    							ImagePullPolicy: "Always",
    							Ports: []corev1.ContainerPort{
    								{
    									ContainerPort: 8080,
    								},
    							},
    						},
    					},
    				},
    			},
    		},
    	}
    
    	if err := r.Create(ctx, &deployment); err != nil {
    		log.Error(err, "创建 Deployment resource 出错")
    		return ctrl.Result{}, err
    	}
    
    	return ctrl.Result{}, nil
    }
    
    func (r *VirtulMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
    	return ctrl.NewControllerManagedBy(mgr).
    		For(&infrav1.VirtulMachine{}).
    		Complete(r)
    }
    

    二、运行main.go文件,

    三、查看集群内的deployment

    [root@k8s-master samples]# kubectl get deployments.apps 
    NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
    centos-deployment      2/2     2            2           22d
    nginx-deployment       3/3     3            3           21d
    qperf-server           1/1     1            1           16d
    virtulmachine-sample   1/1     1            1           17h
    
  • 相关阅读:
    [leetcode] Bulls and Cows
    Win7 系统所有应用颜色调整
    一道题反映Java的类初始化过程
    翻转二叉树(深搜-先序遍历-交换Node)
    在一个数组中,除了两个数外,其余数都是两两成对出现,找出这两个数,要求时间复杂度O(n),空间复杂度O(1)
    一道随机函数题:由rand5()生成rand7()
    求一条直线通过的最大点数
    菜根谭#236
    菜根谭#235
    菜根谭#234
  • 原文地址:https://www.cnblogs.com/wuchangblog/p/14245219.html
Copyright © 2011-2022 走看看