Running Kubernetes Sample Controller

4/26/2019

I am trying to run the Kubernetes sample controller example by following the link https://github.com/kubernetes/sample-controller. I have the repo set up on an Ubuntu 18.04 system and was able to build the sample-controller package. However, when I try to run the go package, I am getting some errors and am unable to debug the issue. Can someone please help me with this?

Here are the steps that I followed :

user@ubuntu-user:~$ go get k8s.io/sample-controller
user@ubuntu-user:~$ cd $GOPATH/src/k8s.io/sample-controller

Here's the error that I get on running the controller:

user@ubuntu-user:~/go/src/k8s.io/sample-controller$ ./sample-controller -kubeconfig=$HOME/.kube/config
E0426 15:05:57.721696   31517 reflector.go:125] k8s.io/sample-controller/pkg/generated/informers/externalversions/factory.go:117: Failed to list *v1alpha1.Foo: the server could not find the requested resource (get foos.samplecontroller.k8s.io)

Kubectl Version :

user@ubuntu-user:~$ kubectl version 
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:11:31Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:02:58Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"```
-- LM10
kubernetes

2 Answers

4/27/2019

Did you run those two commands from readme?

# create a CustomResourceDefinition
kubectl create -f artifacts/examples/crd.yaml

# create a custom resource of type Foo
kubectl create -f artifacts/examples/example-foo.yaml
-- Vasily Angapov
Source: StackOverflow

4/30/2019

I have reproduced your issue. The order of commands in this tutorial is wrong.

In this case you received this error due to lack of resource (samplecontroller)

$ ./sample-controller -kubeconfig=$HOME/.kube/config
E0430 12:55:05.089624  147744 reflector.go:125] k8s.io/sample-controller/pkg/generated/informers/externalversions/factory.go:117: Failed to list *v1alpha1.Foo: the server could not find the requested resource (get foos.samplecontroller.k8s.io)
^CF0430 12:55:05.643778  147744 main.go:74] Error running controller: failed to wait for caches to sync
goroutine 1 [running]:
k8s.io/klog.stacks(0xc0002feb00, 0xc000282200, 0x66, 0x1f5)
    /usr/local/google/home/user/go/src/k8s.io/klog/klog.go:840 +0xb1
k8s.io/klog.(*loggingT).output(0x2134040, 0xc000000003, 0xc0002e12d0, 0x20afafb, 0x7, 0x4a, 0x0)
    /usr/local/google/home/user/go/src/k8s.io/klog/klog.go:791 +0x303
k8s.io/klog.(*loggingT).printf(0x2134040, 0x3, 0x14720f2, 0x1c, 0xc0003c1f48, 0x1, 0x1)
    /usr/local/google/home/user/go/src/k8s.io/klog/klog.go:690 +0x14e
k8s.io/klog.Fatalf(...)
    /usr/local/google/home/user/go/src/k8s.io/klog/klog.go:1241
main.main()
    /usr/local/google/home/user/go/src/k8s.io/sample-controller/main.go:74 +0x3f5

You can verify that this api was not created

$ kubectl api-versions | grep sample
$ <emptyResult>

In the tutorial you have command to create Custom Resource Definition

$ kubectl create -f artifacts/examples/crd.yaml
customresourcedefinition.apiextensions.k8s.io/foos.samplecontroller.k8s.io created

Now you can search this CRD, it will be on the list now.

$ kubectl api-versions | grep sample
samplecontroller.k8s.io/v1alpha1

Next step is to create Foo resource

$ kubectl create -f artifacts/examples/example-foo.yaml
foo.samplecontroller.k8s.io/example-foo created

Those commands will not create any objects yet.

user@user:~/go/src/k8s.io/sample-controller$ kubectl get all
NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   XX.XXX.XXX.XX   <none>        443/TCP   14d

All resources will be created after you will run ./sample-controller -kubeconfig=$HOME/.kube/config

user@user:~/go/src/k8s.io/sample-controller$ ./sample-controller -kubeconfig=$HOME/.kube/config
user@user:~/go/src/k8s.io/sample-controller$ kubectl get all
NAME                               READY   STATUS    RESTARTS   AGE
pod/example-foo-6cbc69bf5d-8k59h   1/1     Running   0          43s

NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.39.240.1   <none>        443/TCP   14d

NAME                          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/example-foo   1         1         1            1           43s

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.apps/example-foo-6cbc69bf5d   1         1         1       43s

Correct order:

$ go get k8s.io/sample-controller
$ cd $GOPATH/src/k8s.io/sample-controller
$ go build -o sample-controller .
$ kubectl create -f artifacts/examples/crd.yaml
$ kubectl create -f artifacts/examples/example-foo.yaml
$ ./sample-controller -kubeconfig=$HOME/.kube/config
$ kubectl get deployments
-- PjoterS
Source: StackOverflow