Skip to content

Commit cfaf0b7

Browse files
committed
fix: load ucloud env file when it exists
2 parents b56659a + cc6b092 commit cfaf0b7

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

cmd/cnivpc/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containernetworking/cni/pkg/version"
3636
"github.com/containernetworking/plugins/pkg/ip"
3737
"github.com/containernetworking/plugins/pkg/ns"
38+
"github.com/joho/godotenv"
3839
"github.com/vishvananda/netlink"
3940
)
4041

@@ -313,6 +314,8 @@ func tickSuicide(done chan bool) {
313314
}
314315
}
315316

317+
const envFilePath = "/etc/kubernetes/ucloud"
318+
316319
func main() {
317320
// Print version
318321
if len(os.Args) == 2 && os.Args[1] == "version" {
@@ -322,6 +325,20 @@ func main() {
322325

323326
ulog.BinaryMode("/var/log/cnivpc.log")
324327

328+
_, err := os.Stat(envFilePath)
329+
if err != nil {
330+
if os.IsNotExist(err) {
331+
ulog.Warnf("Env file %s not found", envFilePath)
332+
} else {
333+
ulog.Errorf("Check env file %s error: %v", envFilePath, err)
334+
}
335+
} else {
336+
err = godotenv.Load(envFilePath)
337+
if err != nil {
338+
ulog.Errorf("Load env file %s error: %v", envFilePath, err)
339+
}
340+
}
341+
325342
about := fmt.Sprintf("ucloud-uk8s-cnivpc version %s", vs.CNIVersion)
326343

327344
done := make(chan bool, 1)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/containernetworking/plugins v0.9.1
99
github.com/coreos/go-iptables v0.6.0
1010
github.com/j-keck/arping v1.0.2
11+
github.com/joho/godotenv v1.5.1
1112
github.com/mattn/go-shellwords v1.0.12
1213
github.com/sirupsen/logrus v1.6.0
1314
github.com/spf13/cobra v1.7.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
8888
github.com/j-keck/arping v0.0.0-20160618110441-2cf9dc699c56/go.mod h1:ymszkNOg6tORTn+6F6j+Jc8TOr5osrynvN6ivFWZ2GA=
8989
github.com/j-keck/arping v1.0.2 h1:hlLhuXgQkzIJTZuhMigvG/CuSkaspeaD9hRDk2zuiMI=
9090
github.com/j-keck/arping v1.0.2/go.mod h1:aJbELhR92bSk7tp79AWM/ftfc90EfEi2bQJrbBFOsPw=
91+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
92+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
9193
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
9294
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
9395
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

0 commit comments

Comments
 (0)