《Kubernetes Operator开发进阶》将在2022年12月由机械工业出版社出版。
本仓库包含书中的3个演示项目代码:
- 第二章 Application Operator Demo
- 第四章 client-go 使用示例
- 第七章 Application Operator
纸质书,2023年1月初出版
测试了很久,主要是版本问题,导致各种问题不断。现在把一个比较好的版本放这里,ubuntu和linux下开发的同学可以参考我readme仓库里面的版本信息。
我的集群是1.23.0版本
使用作者的Dockerfile发现了一些问题:
FROM busybox
COPY ./in-cluster /in-cluster
USER 65532:65532
ENTRYPOINT /in-cluster
执行kubectl run in-cluster -i --image=docker.io/in-cluster:v0.0.1 --restart=Never --overrides='{"spec": {"imagePullSecrets": [{"name": "image-pull-secret"}]}}'会报错:
pod default/in-cluster terminated (Error)
单从image的size上来说,busybox更小。不过busybox默认的libc实现是uClibc,而我们通常运行环境使用的libc实现都是glibc,因此我们要么选择静态编译程序,要么使用busybox:glibc镜像作为base image。
所以我把Dockerfile改为:
FROM busybox:glibc
WORKDIR /
COPY in-cluster .
ENTRYPOINT ["./in-cluster"]
就可以正常执行程序了。
书籍150页。 代码逻辑写完,执行make install后,可以查询到crd,但是查询不到application资源,应该是需要编写application资源的yaml文件,然后创建。yaml不知道咋写,各位大佬支援下呢,谢谢!
$ kubectl get crd applications.apps.tanlay.com
NAME CREATED AT
applications.apps.tanlay.com 2023-01-29T12:57:45
$ kubectl get application
No resources found in default namespace.
apiVersion: apps.tanlay.com/v1
kind: Application
metadata:
name: application-sample
spec:
# TODO(user): Add fields here
deployment:
...
service:
...
原文为
...
GOOS=linux go build -o ./in-cluster .
根据上一步的Dockerfile,应改为
...
GOOS=linux go build -o ./app .
UpdateFunc部分应该是Service对象,不是Application吧
Owns(&corev1.Service{}, builder.WithPredicates(predicate.Funcs{
...
UpdateFunc: func(event event.UpdateEvent) bool {
if event.ObjectNew.GetResourceVersion() == event.ObjectOld.GetResourceVersion() {
return false
}
if reflect.DeepEqual(event.ObjectNew.(*v1.Application).Spec, event.ObjectOld.(*v1.Application).Spec) {
return false
}
return true
},
})).
Owns(&corev1.Service{}, builder.WithPredicates(predicate.Funcs{
...
UpdateFunc: func(event event.UpdateEvent) bool {
if event.ObjectNew.GetResourceVersion() == event.ObjectOld.GetResourceVersion() {
return false
}
if reflect.DeepEqual(event.ObjectNew.(*corev1.Service).Spec, event.ObjectOld.(*corev1.Service).Spec) {
return false
}
return true
},
})).
/data/gomod/pkg/mod/sigs.k8s.io/[email protected]/pkg/log/deleg.go:79:12: undefined: logr.WithCallDepth
/data/gomod/pkg/mod/sigs.k8s.io/[email protected]/pkg/log/deleg.go:163:2: cannot use res (type *DelegatingLogger) as type logr.Logger in return argument
/data/gomod/pkg/mod/sigs.k8s.io/[email protected]/pkg/log/deleg.go:179:2: cannot use res (type *DelegatingLogger) as type logr.Logger in return argument
P69 中“我们可以直接打开staging/k8s.io/client-go"正确目录应为”staging/src/k8s.io/client-go“
我的代码:
// CoffeeSpec defines the desired state of Coffee
type CoffeeSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Replicas int32 `json:"replicas,omitempty"`
Template corev1.PodTemplateSpec `json:"template,omitempty"`
}
我的环境
错误
* spec.validation.openAPIV3Schema.properties[spec].properties[template].properties[spec].properties[volumes].items.properties[ephemeral].properties[volumeClaimTemplate].properties[spec].properties[resources].properties[claims].items.x-kubernetes-map-type: Invalid value: "null": must be atomic as item of a list with x-kubernetes-list-type=set
* spec.validation.openAPIV3Schema.properties[spec].properties[template].properties[spec].properties[ephemeralContainers].items.properties[resources].properties[claims].items.x-kubernetes-map-type: Invalid value: "null": must be atomic as item of a list with x-kubernetes-list-type=set
* spec.validation.openAPIV3Schema.properties[spec].properties[template].properties[spec].properties[containers].items.properties[resources].properties[claims].items.x-kubernetes-map-type: Invalid value: "null": must be atomic as item of a list with x-kubernetes-list-type=set
* spec.validation.openAPIV3Schema.properties[spec].properties[template].properties[spec].properties[initContainers].items.properties[resources].properties[claims].items.x-kubernetes-map-type: Invalid value: "null": must be atomic as item of a list with x-kubernetes-list-type=set
make: *** [install] Error 1
```
报错如下:
$ kubectl apply -f config/samples/apps_v2_application.yaml
Error from server: error when retrieving current configuration of:
Resource: "apps.tanlay.com/v2, Resource=applications", GroupVersionKind: "apps.tanlay.com/v2, Kind=Application"
Name: "nginx-sample", Namespace: "default"
from server for: "config/samples/apps_v2_application.yaml": conversion webhook for apps.tanlay.com/v1, Kind=Application failed: the server could not find the requested resource
api/v2/application_types.go文件
// 只修改了ApplicationSpec结构体的Deployment字段为Workflow,其他和v1版本一致,
// 只在v1版本增加了持久化 //+kubebuilder:storageversion
type ApplicationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Workflow v1.DeploymentTemplate `json:"workflow,omitempty"`
Service v1.ServiceTemplate `json:"service,omitempty"`
}
dappsv1 "github.com/daniel-hutao/application-operator/api/v1"
this links is error, when run "make run", it will be run "go get github.com/daniel-hutao/application-operator/api/v1"
How do I understand the api of this ”dappsv1“
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.