Skip to content

Commit

Permalink
calico-node-agent added
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi committed Nov 13, 2020
0 parents commit 2071d5f
Show file tree
Hide file tree
Showing 13 changed files with 1,094 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# ---> Go
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
*.swp

# Folders
_obj
_test

.VERSION

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
_output
bin
/relay
/structargtest
.idea
.ropeproject

management-state/
static
pkg/generated
config.yaml
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
ROOT_DIR = $(CURDIR)
OUTPUT_DIR = $(ROOT_DIR)/_output
BIN_DIR = $(OUTPUT_DIR)/bin

GO_BUILD := go build

CMDS := $(shell find $(ROOT_DIR)/cmd -mindepth 1 -maxdepth 1 -type d)

build: clean
@for CMD in $(CMDS); do \
echo build $$CMD; \
$(GO_BUILD) -o $(BIN_DIR)/`basename $${CMD}` $$CMD; \
done

clean:
@rm -rf $(OUTPUT_DIR)

.PHONY: build clean
25 changes: 25 additions & 0 deletions cmd/calico-node-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Run test

```
# generate config
$ cat <<EOF > ./config.yaml
nodeName: $(hostname)
ipPools:
- cidr: 192.168.122.34/32
- cidr: 192.168.122.35/32
EOF
# set calico connection environment
$ export DATASTORE_TYPE=kubernetes
$ export KUBECONFIG=~/.kube/config
# start calico-node-agent
$ ./_output/bin/calico-node-agent -conf ./config.yaml
# check calico ipPools
$ calicoctl get ippools -o wide
NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
default-ipv4-ippool 10.40.0.0/16 true Always Never false all()
lzx-t470p-192-168-122-34-32 192.168.122.34/32 false Never Never false kubernetes.io/hostname == "lzx-t470p"
lzx-t470p-192-168-122-35-32 192.168.122.35/32 false Never Never false kubernetes.io/hostname == "lzx-t470p"
```
20 changes: 20 additions & 0 deletions cmd/calico-node-agent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"flag"

"yunion.io/x/kubecomps/pkg/calico-node-agent/serve"
)

var (
conf string
)

func init() {
flag.StringVar(&conf, "conf", "", "config file")
flag.Parse()
}

func main() {
serve.Run(conf)
}
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module yunion.io/x/kubecomps

go 1.15

require (
github.com/projectcalico/libcalico-go v1.7.2-0.20201110235728-977c570b2f4b
k8s.io/api v0.17.2
k8s.io/apimachinery v0.17.2
yunion.io/x/jsonutils v0.0.0-20201110084044-3e4e1cb49769
yunion.io/x/log v0.0.0-20200313080802-57a4ce5966b3
yunion.io/x/pkg v0.0.0-20201028134817-3ed15ee169bc
)
499 changes: 499 additions & 0 deletions go.sum

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions pkg/calico-node-agent/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package client

import (
"os"

"github.com/projectcalico/libcalico-go/lib/apiconfig"
client "github.com/projectcalico/libcalico-go/lib/clientv3"

"yunion.io/x/log"
"yunion.io/x/pkg/errors"
)

const (
DefaultConfigPath = "/etc/calico/calicoctl.cfg"
)

// NewClient creates a new CalicoClient using connection information in the specified
// filename (if it exists), dropping back to environment variables for any
// parameter not loaded from file.
func NewClient(cf string) (client.Interface, error) {
cfg, err := LoadClientConfig(cf)
if err != nil {
return nil, err
}
log.Infof("Loaded client config: %#v", cfg.Spec)

c, err := client.New(*cfg)
if err != nil {
return nil, err
}

return c, err
}

// LoadClientConfig loads the client config from file if the file exists,
// otherwise will load from environment variables.
func LoadClientConfig(cf string) (*apiconfig.CalicoAPIConfig, error) {
if _, err := os.Stat(cf); err != nil {
if cf != DefaultConfigPath {
log.Errorf("Error reading config file: %s\n", cf)
return nil, errors.Wrapf(err, "reading config file %s", cf)
}
log.Warningf("Config file: %s cannot be read, reading config from environment", cf)
cf = ""
}

return apiconfig.LoadClientConfig(cf)
}
44 changes: 44 additions & 0 deletions pkg/calico-node-agent/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"io/ioutil"
"os"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"

"yunion.io/x/kubecomps/pkg/calico-node-agent/types"
)

func GetNodeConfigByFile(cf string) (*types.NodeConfig, error) {
content, err := ioutil.ReadFile(cf)
if err != nil {
return nil, errors.Wrapf(err, "read file %s", cf)
}
return GetNodeConfigByBytes(string(content))
}

func GetNodeConfigByBytes(content string) (*types.NodeConfig, error) {
obj, err := jsonutils.ParseYAML(content)
if err != nil {
return nil, errors.Wrap(err, "parse config file yaml contents")
}
conf := new(types.NodeConfig)
if err := obj.Unmarshal(conf); err != nil {
return nil, errors.Wrap(err, "unmarshal node config")
}
fillNodeConfig(conf)
if err := conf.Validate(); err != nil {
return nil, errors.Wrap(err, "validate node config")
}
return conf, nil
}

func fillNodeConfig(conf *types.NodeConfig) {
if conf.NodeName == "" {
val, ok := os.LookupEnv(types.EnvKeyNodeName)
if ok {
conf.NodeName = val
}
}
}
Loading

0 comments on commit 2071d5f

Please sign in to comment.