Skip to content

Commit

Permalink
pkg/k8s: refactor constants to a dedicated package
Browse files Browse the repository at this point in the history
This will allow to import constant names from other packages without
worrying on importing k8s dependencies.

Signed-off-by: André Martins <andre@cilium.io>
  • Loading branch information
aanm committed May 29, 2020
1 parent 1439b27 commit 93ab013
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cilium/cmd/preflight_identity_crd_migrate.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/cilium/cilium/pkg/idpool"
"github.com/cilium/cilium/pkg/k8s"
k8sconfig "github.com/cilium/cilium/pkg/k8s/config"
k8sConst "github.com/cilium/cilium/pkg/k8s/constants"
"github.com/cilium/cilium/pkg/k8s/identitybackend"
"github.com/cilium/cilium/pkg/kvstore"
kvstoreallocator "github.com/cilium/cilium/pkg/kvstore/allocator"
Expand Down Expand Up @@ -200,7 +201,7 @@ func initK8s(ctx context.Context) (crdBackend allocator.Backend, crdAllocator *a
log.WithError(err).Fatal("Unable to connect to Kubernetes apiserver")
}

if err := k8s.GetNodeSpec(os.Getenv(k8s.EnvNodeNameSpec)); err != nil {
if err := k8s.GetNodeSpec(os.Getenv(k8sConst.EnvNodeNameSpec)); err != nil {
log.WithError(err).Fatal("Unable to connect to get node spec from apiserver")
}

Expand Down
3 changes: 2 additions & 1 deletion daemon/cmd/daemon.go
Expand Up @@ -47,6 +47,7 @@ import (
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
"github.com/cilium/cilium/pkg/ipcache"
"github.com/cilium/cilium/pkg/k8s"
k8sConst "github.com/cilium/cilium/pkg/k8s/constants"
"github.com/cilium/cilium/pkg/k8s/watchers"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
Expand Down Expand Up @@ -414,7 +415,7 @@ func NewDaemon(ctx context.Context, dp datapath.Datapath) (*Daemon, *endpointRes
d.nodeDiscovery.UpdateCiliumNodeResource()
}

if err := k8s.GetNodeSpec(os.Getenv(k8s.EnvNodeNameSpec)); err != nil {
if err := k8s.GetNodeSpec(os.Getenv(k8sConst.EnvNodeNameSpec)); err != nil {
log.WithError(err).Fatal("Unable to connect to get node spec from apiserver")
}

Expand Down
12 changes: 2 additions & 10 deletions pkg/k8s/const.go → pkg/k8s/constants/const.go
@@ -1,4 +1,4 @@
// Copyright 2016-2017 Authors of Cilium
// Copyright 2016-2020 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,17 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package k8s

import (
"time"
)
package constants

const (
// BackOffLoopTimeout is the default duration when trying to reach the
// kube-apiserver.
BackOffLoopTimeout = 2 * time.Minute

// EnvNodeNameSpec is the environment label used by Kubernetes to
// specify the node's name.
EnvNodeNameSpec = "K8S_NODE_NAME"
Expand Down
3 changes: 2 additions & 1 deletion pkg/k8s/init.go
Expand Up @@ -25,6 +25,7 @@ import (
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
cilium_v2_client "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2/client"
k8sconfig "github.com/cilium/cilium/pkg/k8s/config"
k8sConst "github.com/cilium/cilium/pkg/k8s/constants"
slim_corev1 "github.com/cilium/cilium/pkg/k8s/slim/k8s/apis/core/v1"
k8sversion "github.com/cilium/cilium/pkg/k8s/version"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -190,7 +191,7 @@ func Init(conf k8sconfig.Configuration) error {
func GetNodeSpec(nodeName string) error {
if nodeName == "" {
if option.Config.K8sRequireIPv4PodCIDR || option.Config.K8sRequireIPv6PodCIDR {
return fmt.Errorf("node name must be specified via environment variable '%s' to retrieve Kubernetes PodCIDR range", EnvNodeNameSpec)
return fmt.Errorf("node name must be specified via environment variable '%s' to retrieve Kubernetes PodCIDR range", k8sConst.EnvNodeNameSpec)
}
return nil
}
Expand Down

0 comments on commit 93ab013

Please sign in to comment.