k8s 自定义informer

it clone https://github.com/kairen/controller101.git

root@ubuntu:~/controller101# make
go build -ldflags="-s -w -X github.com/cloud-native-taiwan/controller101/pkg/version.version=v0.1.0" 
  -a -o out/controller cmd/main.go
go: downloading k8s.io/client-go v0.0.0-20191005115821-b1fd78950135
go: downloading github.com/spf13/pflag v1.0.3
go: downloading k8s.io/apimachinery v0.0.0-20191005115455-e71eb83a557c
go: downloading github.com/thoas/go-funk v0.4.0
go: downloading github.com/docker/go-connections v0.4.0
go: downloading github.com/pkg/errors v0.8.1
go: downloading golang.org/x/net v0.0.0-20190812203447-cdfb69ac37fc
go: downloading github.com/docker/go-units v0.4.0
go: downloading golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db
go: downloading k8s.io/api v0.0.0-20191005115622-2e41325d9e4b
go: downloading golang.org/x/time v0.0.0-20161028155119-f51c12702a4d
go: downloading k8s.io/utils v0.0.0-20190920012459-5008bf6f8cd6
go: downloading gopkg.in/yaml.v2 v2.2.4
go: downloading golang.org/x/crypto v0.0.0-20181025213731-e84da0312774
go: downloading golang.org/x/oauth2 v0.0.0-20190402181905-9f3314589c9a
go: downloading golang.org/x/sys v0.0.0-20190209173611-3b5209105503
root@ubuntu:~/controller101# go run cmd/main.go --kubeconfig=$HOME/.kube/config -v=2 --logtostderr  --vm-driver=docker  --lease-lock-namespace=default
I0708 11:46:39.735032   28294 leaderelection.go:242] attempting to acquire leader lease  default/controller101...
I0708 11:46:39.769316   28294 leaderelection.go:252] successfully acquired lease default/controller101
I0708 11:46:39.769415   28294 controller.go:77] Starting the controller
I0708 11:46:39.769439   28294 controller.go:78] Waiting for the informer caches to sync
E0708 11:46:39.771949   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:40.773790   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:41.775497   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:42.777139   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:43.778515   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:44.779683   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:45.781476   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)
E0708 11:46:46.782890   28294 reflector.go:153] pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/reflector.go:105: Failed to list *v1alpha1.VirtualMachine: the server could not find the requested resource (get virtualmachines.cloudnative.tw)

 

root@ubuntu:~/kube-batch/pkg# cat <<EOF | kubectl apply -f -
> apiVersion: cloudnative.tw/v1alpha1
> kind: VirtualMachine
> metadata:
>   name: test-vm
> spec:
>   resource:
>     cpu: 2
>     memory: 4G
> EOF
virtualmachine.cloudnative.tw/test-vm createderror: unable to recognize "STDIN": no matches for kind "VirtualMachine" in version "cloudnative.tw/v1alpha1"
root@ubuntu:~/kube-batch/pkg# cd ~
root@ubuntu:~# cat<<EOF >vm.yaml
>  
> apiVersion: cloudnative.tw/v1alpha1
> kind: VirtualMachine
> metadata:
>   name: test-vm
> spec:
>   resource:
>     cpu: 2
>     memory: 4G
> EOF
root@ubuntu:~# kubectl apply -f vm.yaml 
error: unable to recognize "vm.yaml": no matches for kind "VirtualMachine" in version "cloudnative.tw/v1alpha1"
root@ubuntu:~# cat vm.yaml 
 
apiVersion: cloudnative.tw/v1alpha1
kind: VirtualMachine
metadata:
  name: test-vm
spec:
  resource:
    cpu: 2
    memory: 4G
root@ubuntu:~# 

排查

root@ubuntu:~/controller101# cat pkg/apis/cloudnative/v1alpha1/doc.go 
/*
Copyright © 2019 The controller101 Authors.

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.
*/

// +k8s:deepcopy-gen=package
// +groupName=cloudnative.tw

// Package v1alpha1 is the v1alpha1 version of the API.
package v1alpha1 // import "github.com/cloud-native-taiwan/controller101/pkg/apis/cloudnative/v1alpha1"
root@ubuntu:~/controller101# 

原来是没有kubectl apply -f crd.yml 

root@ubuntu:~/controller101/deploy# cat crd.yml 
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: virtualmachines.cloudnative.tw
spec:
  group: cloudnative.tw
  version: v1alpha1
  names:
    kind: VirtualMachine
    singular: virtualmachine
    plural: virtualmachines
    shortNames:
    - vm
    - vms
  scope: Namespaced
  additionalPrinterColumns:
  - name: Status
    type: string
    JSONPath: .status.phase
  - name: CPU
    type: number
    JSONPath: .status.server.usage.cpu
  - name: Memory
    type: number
    JSONPath: .status.server.usage.memory
  - name: Age
    type: date
    JSONPath: .metadata.creationTimestamproot@ubuntu:~/controller101/deploy# kubectl apply  crd.yml 
error: must specify one of -f and -k


root@ubuntu:
~/controller101/deploy# kubectl apply -f crd.yml customresourcedefinition.apiextensions.k8s.io/virtualmachines.cloudnative.tw created root@ubuntu:~/controller101/deploy# out/controller --kubeconfig=$HOME/.kube/config -v=2 --logtostderr --vm-driver=docker --lease-lock-namespace=default -bash: out/controller: No such file or directory root@ubuntu:~/controller101/deploy# cd .. root@ubuntu:~/controller101# out/controller --kubeconfig=$HOME/.kube/config -v=2 --logtostderr --vm-driver=docker --lease-lock-namespace=default I0708 15:00:47.471454 55864 leaderelection.go:242] attempting to acquire leader lease default/controller101... I0708 15:00:47.494822 55864 leaderelection.go:252] successfully acquired lease default/controller101 I0708 15:00:47.495195 55864 controller.go:77] Starting the controller I0708 15:00:47.495287 55864 controller.go:78] Waiting for the informer caches to sync I0708 15:00:47.595802 55864 controller.go:86] Started workers I0708 15:00:47.595834 55864 main.go:144] : leading
root@ubuntu:~# kubectl apply -f vm.yaml 
virtualmachine.cloudnative.tw/test-vm created
root@ubuntu:~# 
root@ubuntu:~/controller101# out/controller  --kubeconfig=$HOME/.kube/config -v=2 --logtostderr  --vm-driver=docker  --lease-lock-namespace=default
I0708 15:00:47.471454   55864 leaderelection.go:242] attempting to acquire leader lease  default/controller101...
I0708 15:00:47.494822   55864 leaderelection.go:252] successfully acquired lease default/controller101
I0708 15:00:47.495195   55864 controller.go:77] Starting the controller
I0708 15:00:47.495287   55864 controller.go:78] Waiting for the informer caches to sync
I0708 15:00:47.595802   55864 controller.go:86] Started workers
I0708 15:00:47.595834   55864 main.go:144] : leading
I0708 15:03:09.027577   55864 controller.go:122] Controller successfully synced 'default/test-vm'
E0708 15:03:11.903690   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
E0708 15:03:14.770912   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
E0708 15:03:17.759119   55864 controller.go:127] Controller error syncing 'default/test-vm': Operation cannot be fulfilled on virtualmachines.cloudnative.tw "test-vm": the object has been modified; please apply your changes to the latest version and try again, requeuing
E0708 15:03:20.639222   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
E0708 15:03:23.572853   55864 controller.go:127] Controller error syncing 'default/test-vm': Operation cannot be fulfilled on virtualmachines.cloudnative.tw "test-vm": the object has been modified; please apply your changes to the latest version and try again, requeuing
E0708 15:03:26.461394   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
E0708 15:03:29.372056   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
E0708 15:03:32.249638   55864 controller.go:127] Controller error syncing 'default/test-vm': Operation cannot be fulfilled on virtualmachines.cloudnative.tw "test-vm": the object has been modified; please apply your changes to the latest version and try again, requeuing
E0708 15:03:35.229757   55864 controller.go:127] Controller error syncing 'default/test-vm': Error: No such image: nginx:1.17.4, requeuing
I0708 15:03:39.934594   55864 controller.go:122] Controller successfully synced 'default/test-vm'
I0708 15:03:39.935948   55864 controller.go:122] Controller successfully synced 'default/test-vm'
I0708 15:03:39.935987   55864 controller.go:122] Controller successfully synced 'default/test-vm'
I0708 15:03:47.499369   55864 controller.go:122] Controller successfully synced 'default/test-vm'

 AddFunc

 

root@ubuntu:~/controller101# dlv attach 39568
Type 'help' for list of commands.
(dlv) b controller.enqueue
Breakpoint 1 (enabled) set at 0xad7f10 for github.com/cloud-native-taiwan/controller101/pkg/controller.(*Controller).enqueue() ./pkg/controller/controller.go:133
(dlv) c

创建

root@ubuntu:~# kubectl apply -f vm.yaml
virtualmachine.cloudnative.tw/test-vm created

> github.com/cloud-native-taiwan/controller101/pkg/controller.(*Controller).enqueue() ./pkg/controller/controller.go:133 (hits goroutine(30):1 total:1) (PC: 0xad7f10)
Warning: debugging optimized function
   128:                 return true
   129:         }
   130:         return true
   131: }
   132:
=> 133: func (c *Controller) enqueue(obj interface{}) {
   134:         key, err := cache.MetaNamespaceKeyFunc(obj)
   135:         if err != nil {
   136:                 utilruntime.HandleError(err)
   137:                 return
   138:         }
(dlv) bt
 0  0x0000000000ad7f10 in github.com/cloud-native-taiwan/controller101/pkg/controller.(*Controller).enqueue
    at ./pkg/controller/controller.go:133
 1  0x0000000000ad9558 in github.com/cloud-native-taiwan/controller101/pkg/controller.(*Controller).enqueue-fm
    at ./pkg/controller/controller.go:133
 2  0x0000000000aca538 in k8s.io/client-go/tools/cache.(*ResourceEventHandlerFuncs).OnAdd
    at /opt/gopath/pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/controller.go:198
 3  0x0000000000ac89e0 in k8s.io/client-go/tools/cache.(*processorListener).run.func1.1
    at /opt/gopath/pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/shared_informer.go:658
 4  0x0000000000aaf050 in k8s.io/apimachinery/pkg/util/wait.ExponentialBackoff
    at /opt/gopath/pkg/mod/k8s.io/apimachinery@v0.0.0-20191005115455-e71eb83a557c/pkg/util/wait/wait.go:292
 5  0x0000000000ac8b88 in k8s.io/client-go/tools/cache.(*processorListener).run.func1
    at /opt/gopath/pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/shared_informer.go:652
 6  0x0000000000aaf774 in k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1
    at /opt/gopath/pkg/mod/k8s.io/apimachinery@v0.0.0-20191005115455-e71eb83a557c/pkg/util/wait/wait.go:152
 7  0x0000000000aaed10 in k8s.io/apimachinery/pkg/util/wait.JitterUntil
    at /opt/gopath/pkg/mod/k8s.io/apimachinery@v0.0.0-20191005115455-e71eb83a557c/pkg/util/wait/wait.go:153
 8  0x0000000000ac3dc4 in k8s.io/apimachinery/pkg/util/wait.Until
    at /opt/gopath/pkg/mod/k8s.io/apimachinery@v0.0.0-20191005115455-e71eb83a557c/pkg/util/wait/wait.go:88
 9  0x0000000000ac3dc4 in k8s.io/client-go/tools/cache.(*processorListener).run
    at /opt/gopath/pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/shared_informer.go:650
10  0x0000000000aca3e8 in k8s.io/client-go/tools/cache.(*processorListener).run-fm
    at /opt/gopath/pkg/mod/k8s.io/client-go@v0.0.0-20191005115821-b1fd78950135/tools/cache/shared_informer.go:644
11  0x0000000000aaf6d4 in k8s.io/apimachinery/pkg/util/wait.(*Group).Start.func1
    at /opt/gopath/pkg/mod/k8s.io/apimachinery@v0.0.0-20191005115455-e71eb83a557c/pkg/util/wait/wait.go:71
12  0x00000000000719b4 in runtime.goexit
    at /usr/local/go/src/runtime/asm_arm64.s:1148
133 func (c *Controller) enqueue(obj interface{}) {
134         key, err := cache.MetaNamespaceKeyFunc(obj)
135         if err != nil {
136                 utilruntime.HandleError(err)
137                 return
138         }
139         c.queue.Add(key)
140 }
原文地址:https://www.cnblogs.com/dream397/p/14985457.html