What's wrong about kuberent client demo error - go

download kubernetes sdk source code
git clone https://github.com/kubernetes/client-go.git kubernet-client
and then
cd kubernet-client/examples/in-cluster-client-configuration/
and
type go run main.go , has error
➜ in-cluster-client-configuration git:(master) go run main.go
# net
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:351:11: d.DialContext undefined (type *Dialer has no field or method DialContext)
/usr/local/Cellar/go/1.18.2/libexec/src/net/dial.go:372:18: invalid character U+2013 '–' in identifier
/usr/local/Cellar/go/1.18.2/libexec/src/net/lookup_unix.go:70:14: d.DialContext undefined (type Dialer has no field or method DialContext)
➜ in-cluster-client-configuration git:(master) pwd
go_sourcecode/kubernet-client/examples/in-cluster-client-configuration
the go version
➜ in-cluster-client-configuration git:(master) go version
go version go1.18.2 darwin/amd64
what's wrong?

The step-by-step process has been written in the markdown file (https://github.com/kubernetes/client-go/tree/master/examples/in-cluster-client-configuration), why you didn't follow that ?
As this is an in-cluster example, this has to be run from inside of a kubernetes cluster. go run main.go will not work. To run this,
make a binary : go build -o ./app .
build an image : docker build -t YOUR_DOCKER_ID/controller:in-cluster .
push it on docker hub: docker push IMAGE
OR load this image into cluster. For kind cluster : kind load docker-image IMAGE
give permission to list pods : kubectl create clusterrolebinding default-view --clusterrole=view --serviceaccount=default:default
Then run kubectl run --rm -i demo --image=IMAGE

Related

Error while exec kubectl get po -o yaml in golang example

How to get Kubectl get po -o yaml in golang
it to run this code
Go client example
where the expected output is
./app
There are 3 pods in the cluster
There are 3 pods in the cluster
There are 3 pods in the cluster
but when i run it i got
go build -o app .
./app
panic: exec plugin: invalid apiVersion "client.authentication.k8s.io/v1alpha1"
goroutine 1 [running]:
main.main()
/Users/padmanabanpr/Documents/client-go/examples/out-of-cluster-client-configuration/main.go:61 +0x5b6
This might be due to your k8s client/server version being higher than 1.24+
https://github.com/aws/aws-cli/issues/6920 Please refer this for further clarification
the following steps fixed the issue
install Kubectl client 1.23.0-00(there is some problem with 1.24.x)
$ cd client-go/examples/out-of-cluster-client-configuration
$ go mod init example.com/m
$ go mod tidy
$ go build -o app .
$ ./app

Golang linter issues 'context loading failed: no go files to analyze'

We are using
golangci-lint version 1.40.1 together with
golang version 1.16.4
in our project for linting our Golang code.
Until now, what we did is running this bash script (from the root
directory of our repository):
if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Fetching linter..."
go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy
fi
golangci-lint run --build-tags="unit contract container"
With some recent updates of Golang and golangci-lint, we suddenly face this error message:
ERRO Running error: context loading failed: no go files to analyze
There is a lengthy post on GitHub regarding this issue but the only useful suggestion there is to turn off the GO111MODULE env variable. When I run the linter with GO111MODULE turned off like
GO111MODULE=off golangci-lint run --build-tags="unit contract container"
the upper error message disappears but instead I am getting lots of false linting errors like:
api/router.go:152:5: undeclared name: `PermissionUpdatePackage` (typecheck)
PermissionUpdatePackage,
^
My go environment looks like this:
GO111MODULE=on
GOPATH=/Users/USER/workspace/go
GOROOT=/usr/local/opt/go/libexec
GOPRIVATE=*.CUSTOMER.com
GOSS_PATH=/usr/local/bin/goss
I tried to install the linter via go get... as well as go install ... and finally brew install golangci-lint which seems to be the recommended way following this documentation.
Running a go get ./... in the root of the project eventually solved the issues. In between we ran the following commands that probably cleared some (module?) caches that might have caused trouble as well:
golangci-lint cache clean && go clean -modcache -cache -i
golangci-lint run -v --timeout=5s
The error message
ERRO Running error: context loading failed: failed to load packages: timed out to load packages: context deadline exceeded
in the latter command pointed us to this GitHub post that made me try out go get ./...
For installing the linter (with a specified version), we ended up with this script:
linter_version='1.40.1'
if ! [ -x "$(command -v golangci-lint)" ]; then
echo "Fetching linter..."
# we cannot install linter in the project directory, otherwise we get dependency errors
# hence, temporarily jump into the /tmp directory
pushd /tmp > /dev/null
GO111MODULE=on go get github.com/golangci/golangci-lint/cmd/golangci-lint#v"${linter_version}" 2>&1
popd >/dev/null
fi

Error while running make command using Fabric 1.0.6 after all the 15 steps

I am installing fabric in a custom path $HOME//gopath/src/github.com/hyperledger/fabric using the make command but after installing all the 15 steps, I am getting the following error
Step 15/15 : LABEL org.hyperledger.fabric.version=1.0.2 org.hyperledger.fabric.base.version=0.3.2
---> Running in ed05a67810db
Removing intermediate container ed05a67810db
---> 99b4b0b28957
Successfully built 99b4b0b28957
Successfully tagged hyperledger/fabric-javaenv:latest
docker tag hyperledger/fabric-javaenv hyperledger/fabric-javaenv:x86_64-1.0.2
build/bin/peer
CGO_CFLAGS=" " GOBIN=/root/gopath/src/github.com/hyperledger/fabric/build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/metadata.Version=1.0.2 -X github.com/hyperledger/fabric/common/metadata.BaseVersion=0.3.2 -X github.com/hyperledger/fabric/common/metadata.BaseDockerLabel=org.hyperledger.fabric -X github.com/hyperledger/fabric/common/metadata.DockerNamespace=hyperledger -X github.com/hyperledger/fabric/common/metadata.BaseDockerNamespace=hyperledger" github.com/hyperledger/fabric/peer
go build github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11: invalid flag in #cgo LDFLAGS: -Wl,--no-as-needed
Makefile:227: recipe for target 'build/bin/peer' failed
make: *** [build/bin/peer] Error 1
Docker version 17.12.0-ce, build c97c6d6
docker-compose version 1.18.0, build 8dd22a9
go version go1.9.4 linux/amd64
OS : Ubuntu 16.04
I have set the following path also
$ mkdir $HOME/gopath
$ export GOPATH=$HOME/gopath
$ export GOROOT=$HOME/go
$ export PATH=$PATH:$GOROOT/bin
Jira Link: https://jira.hyperledger.org/browse/FAB-2218 which I have already done
It is a known issue in go 1.9.4:
https://github.com/golang/go/issues/23739
While go people are fixing it, you can manually whitelist the flags before starting make:
$ export CGO_LDFLAGS_ALLOW='-Wl,--no-as-needed'
$ make
Alternatively you can just downgrade to go 1.9.3
as far as I know you don't need to build the fabric itself.
You just need to have sources in the gopath, as your chaincode will need this, when you are building your chaincode for the blockchain itself.
In addition you will probably also need to install the fabric-ca for your chaincode to build successfully, if you use the certificate-stuff.
This is sufficient to check if you installed fabric correctly:
cd $GOPATH/src/github.com/hyperledger/fabric
make unit-test
Keep going if this works.
I found this confusing at first as well ;)
I'd assume you switch to golang 1.9.4 or greater hence I belive this actually related to following issue, to solve it you need to update vendor dependencies:
govendor fetch github.com/hyperledger/fabric/vendor/github.com/miekg/pkcs11
Next continue to build binaries and run tests.

How can I fix the following Google Cloud Platform deployment error? [Error 2]

I'm trying to deploy my web-app to Google Cloud Platform, and I got an error while deploying. I understand that last package (go-sqlite3) needs gcc compiler, and Google Cloud WM has it:
$which gcc
/usr/bin/gcc
but it won't work
Step 3 : RUN go-wrapper install -tags appenginevm
---> Running in b0f03024342d
+ exec go install -v -tags appenginevm
github.com/mattn/go-colorable
github.com/mattn/go-isatty
github.com/labstack/gommon/color
github.com/valyala/fasttemplate/vendor/github.com/valyala/bytebufferpool
github.com/valyala/fasttemplate
github.com/labstack/gommon/log
golang.org/x/crypto/acme
golang.org/x/crypto/acme/autocert
github.com/labstack/echo
golang.org/x/net/context
github.com/mattn/go-sqlite3
# github.com/mattn/go-sqlite3
exec: "gcc": executable file not found in $PATH
The command '/bin/sh -c go-wrapper install -tags appenginevm' returned a non-zero code: 2
ERROR
ERROR: build step "gcr.io/cloud-builders/docker#sha256:926dc1a14e6f7eb5b3462b5c1d491aa6c73090291167ac2bf181c026b05f19da" failed: exit status 2
You have to make your path correct by following command on your shell.
PATH=/usr/bin:$PATH

Golang Dockerfile Failing

I have a Golang web application that I am looking to run in Docker container. I am able to run it fine outside of the container, so I know it works, but when I build it from Dockerfile and run it, it gives me an error.
The Makefile looks like the following
GOCMD = go
GOBUILD = $(GOCMD) build
GOGET = $(GOCMD) get -v
GOCLEAN = $(GOCMD) clean
GOINSTALL = $(GOCMD) install
GOTEST = $(GOCMD) test
.PHONY: all
all: build
test:
$(GOTEST) -v -cover ./...
build:
$(GOGET); $(GOBUILD) -v -o engine
clean:
$(GOCLEAN) -n -i -x
rm -f $(GOPATH)/bin/engine
rm -rf bin/engine
install:
$(GOINSTALL)
And the Dockerfile looks like the following
FROM golang
ADD engine /go/bin/engine
EXPOSE 7777
ENTRYPOINT /go/bin/engine
I am building the image and running it using the following
docker build -t engine .
docker run -d --name engine -p 7777:7777 engine
and its giving me the following error
/go/bin/engine: 1: /go/bin/engine: Syntax error: "(" unexpected
When you build a binary, go build assumes that you are trying to build for your current computer, it chooses values for GOOS and GOARCH (described here) for you.
If you are not building on a linux machine then you will need to cross compile the binary for linux, as this is what the OS inside the docker container will be running. Explanation here
You need something like:
GOOS=linux
build:
$(GOGET); GOOS=$(GOOS) $(GOBUILD) -v -o engine

Resources