test.go:
package test
import (
"context"
"hfftest/conf"
"github.com/projectcalico/libcalico-go/lib/apiconfig"
"github.com/projectcalico/libcalico-go/lib/clientv3"
"github.com/projectcalico/libcalico-go/lib/options"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var ClientV3 clientv3.Interface
func CalicoSetup() {
var err error
ClientV3, err = clientv3.New(apiconfig.CalicoAPIConfig{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: apiconfig.CalicoAPIConfigSpec{
DatastoreType: apiconfig.EtcdV3,
EtcdConfig: apiconfig.EtcdConfig{
EtcdEndpoints: conf.EtcdEndpoints,
EtcdKeyFile: conf.EtcdKeyFile,
EtcdCertFile: conf.EtcdCertFile,
EtcdCACertFile: conf.EtcdCACertFile,
}},
})
if err != nil {
logrus.Error(err.Error())
return
}
logrus.Info("setup calico client succeed")
}
func Test() {
logrus.Println("my test...")
CalicoSetup()
ctx := context.Background()
ipPools, err := ClientV3.IPPools().List(ctx, options.ListOptions{})
if err != nil {
logrus.Errorln(err)
}
logrus.Infoln(ipPools)
}
conf.go
package conf
var (
EtcdEndpoints = "10.10.0.13:2379"
EtcdKeyFile = "./etcd-key.pem"
EtcdCertFile = "./etcd.pem"
EtcdCACertFile = "./ca.pem"
)
go.mod:
module hfftest
go 1.17
require (
github.com/projectcalico/libcalico-go v1.7.2-0.20200303001954-8d098ee0dd61
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.2.1
k8s.io/apimachinery v0.0.0-20191028221656-72ed19daf4bb
)
require (
cloud.google.com/go v0.81.0 // indirect
github.com/Azure/go-autorest/autorest v0.9.0 // indirect
github.com/Azure/go-autorest/autorest/adal v0.5.0 // indirect
github.com/Azure/go-autorest/autorest/date v0.1.0 // indirect
github.com/Azure/go-autorest/logger v0.1.0 // indirect
github.com/Azure/go-autorest/tracing v0.5.0 // indirect
github.com/coreos/etcd v3.3.18+incompatible // indirect
github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-playground/locales v0.12.1 // indirect
github.com/go-playground/universal-translator v0.0.0-20170327191703-71201497bace // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d // indirect
github.com/gophercloud/gophercloud v0.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.1 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/kelseyhightower/envconfig v0.0.0-20180517194557-dd1402a4d99d // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
github.com/leodido/go-urn v0.0.0-20181204092800-a67a23e1c1af // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/projectcalico/go-json v0.0.0-20161128004156-6219dc7339ba // indirect
github.com/projectcalico/go-yaml-wrapper v0.0.0-20191112210931-090425220c54 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/go-playground/validator.v9 v9.27.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/api v0.0.0-20191114100352-16d7abae0d2a // indirect
k8s.io/client-go v0.0.0-20191114101535-6c5935290e33 // indirect
k8s.io/klog v0.4.0 // indirect
k8s.io/utils v0.0.0-20191114200735-6ca3b61696b6 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
replace google.golang.org/grpc => google.golang.org/grpc v1.25.1
注意grpc版本匹配问题