Operator实践(一)

k8s原生资源是有限的,当遇到某些场景时会发现不够用,此时CRD(Custom Resource Definition,自定义资源)就会派上用场。用户注册自定义的CRD,然后创建对应的资源实例(称为Custom Resource,简称CR),而后通过自己编写Controller来不断地检测当前k8s中所定义的CR的状态,如果状态和预期不一致,则调整。换句话说operator是针对自定义资源的控制器。

要构建operator步骤如下:

1.安装operator-sdk;
安装方式见https://sdk.operatorframework.io/docs/installation/,我这里直接下载operator-sdk_linux_amd64(从https://github.com/operator-framework/operator-sdk/releases/tag/v1.8.1下载),放到/usr/local/bin/目录下,执行 
#operator-sdk version
operator-sdk version: "v1.8.1", commit: "23f76979dd30f26939041d037646b3e08f2fa8c5", kubernetes version: "1.20.2", go version: "go1.16.5", GOOS: "linux", GOARCH: "amd64"
说明operator-sdk已ok。
2.初始化operator
创建一个目录,如new-operator,进入到该目录,执行如下命令:
operator-sdk init --domain=  --repo=.
 --domain string            domain for groups (default "my.domain"),我这里没有定义domain,直接等于空
 --repo string              name to use for go module (e.g., github.com/user/repo), defaults to the go package of the current working directory,我这里直接用的当前目录
注:没有--domain和--repo会报错

第二步执行完后会看到如下目录结构:

3.create api&controller
执行以下命令:
operator-sdk create api --group cloud.com --version v1 --kind Logger --resource --controller
--group string         resource Group,设置资源group
--version string       resource Version,设置资源version
--controller           if set, generate the controller without prompting the user (default true),默认为true
--resource             if set, generate the resource without prompting the user (default true),默认为true

 第三步执行完成后,可以看到如下目录结构:

4.更新resource type
进入“new-operator”, 打开api/v1/resource_types.go, 更新Spec和Status字段, like this
type NewresourceSpec struct {
	// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
	// Important: Run "make" to regenerate code after modifying this file

	// Foo is an example field of Newresource. Edit newresource_types.go to remove/update
	Foo string `json:"foo,omitempty"`
	Name string `json:"name,omitempty"`
	Sex  string `json:"sex,omitempty"`
	Height string `json:"height,omitempty"`
	Class string `json:"class,omitempty"`
}

// NewresourceStatus defines the observed state of Newresource
type NewresourceStatus struct {
	DeployStatus         string       `json:"deployStatus,omitempty" protobuf:"bytes,1,opt,name=deployStatus"`
	DeployDescription    string       `json:"deployDescription,omitempty" protobuf:"bytes,2,opt,name=deployDescription"`
	DeployLastUpdateDate *metav1.Time `json:"deployLastUpdateDate,omitempty" protobuf:"bytes,3,opt,name=deployLastUpdateDate"`
	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
	// Important: Run "make" to regenerate code after modifying this file
}

修改完成后执行make,Run "make" to regenerate code after modifying this file

 

5.生成CRD
执行make manifests
生成的CRD文件位于new-operator\config\crd\bases\cloud.com_newresources.yaml

6.生成CSV(olm部署使用,可选)
执行以下命令:
operator-sdk generate kustomize manifests

注:执行后会提示以下内容,按照提示输入即可。
Display name for the operator (required):
> new-operator

Description for the operator (required):
> new-operator

Provider's name for the operator (required):
> new-operator

Any relevant URL for the provider name (optional):
>

Comma-separated list of keywords for your operator (required):
> new,operator

Comma-separated list of maintainers and their emails (e.g. 'name1:email1, name2:email2') (required):
> ccc:123@163.com
Kustomize files generated successfully

生成的文件位于new-operator\config\manifests\bases\new-operator.clusterserviceversion.yaml

 至此,operator框架初始化完毕,后续填充代码。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SeasonRun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值