-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2071d5f
Showing
13 changed files
with
1,094 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
Oops, something went wrong.