temp commit

This commit is contained in:
magicsong
2019-08-14 20:45:43 +08:00
parent 90fa38851f
commit 7314064e83
635 changed files with 116500 additions and 494 deletions

View File

@@ -0,0 +1,105 @@
// Copyright (c) 2016-2017 Tigera, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apiconfig
import (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiv3 "github.com/projectcalico/libcalico-go/lib/apis/v3"
)
type DatastoreType string
const (
EtcdV3 DatastoreType = "etcdv3"
Kubernetes DatastoreType = "kubernetes"
KindCalicoAPIConfig = "CalicoAPIConfig"
)
// CalicoAPIConfig contains the connection information for a Calico CalicoAPIConfig resource
type CalicoAPIConfig struct {
metav1.TypeMeta `json:",inline"`
// Standard object's metadata.
metav1.ObjectMeta `json:"metadata,omitempty"`
// Specification of the BGPConfiguration.
Spec CalicoAPIConfigSpec `json:"spec,omitempty"`
}
// CalicoAPIConfigSpec contains the specification for a Calico CalicoAPIConfig resource.
type CalicoAPIConfigSpec struct {
DatastoreType DatastoreType `json:"datastoreType" envconfig:"DATASTORE_TYPE" default:"etcdv3"`
// Inline the ectd config fields
EtcdConfig
// Inline the k8s config fields.
KubeConfig
}
type EtcdConfig struct {
EtcdEndpoints string `json:"etcdEndpoints" envconfig:"ETCD_ENDPOINTS"`
EtcdDiscoverySrv string `json:"etcdDiscoverySrv" envconfig:"ETCD_DISCOVERY_SRV"`
EtcdUsername string `json:"etcdUsername" envconfig:"ETCD_USERNAME"`
EtcdPassword string `json:"etcdPassword" envconfig:"ETCD_PASSWORD"`
EtcdKeyFile string `json:"etcdKeyFile" envconfig:"ETCD_KEY_FILE"`
EtcdCertFile string `json:"etcdCertFile" envconfig:"ETCD_CERT_FILE"`
EtcdCACertFile string `json:"etcdCACertFile" envconfig:"ETCD_CA_CERT_FILE"`
// These config file parameters are to support inline certificates, keys and CA / Trusted certificate.
// There are no corresponding environment variables to avoid accidental exposure.
EtcdKey string `json:"etcdKey" ignored:"true"`
EtcdCert string `json:"etcdCert" ignored:"true"`
EtcdCACert string `json:"etcdCACert" ignored:"true"`
}
type KubeConfig struct {
Kubeconfig string `json:"kubeconfig" envconfig:"KUBECONFIG" default:""`
K8sAPIEndpoint string `json:"k8sAPIEndpoint" envconfig:"K8S_API_ENDPOINT" default:""`
K8sKeyFile string `json:"k8sKeyFile" envconfig:"K8S_KEY_FILE" default:""`
K8sCertFile string `json:"k8sCertFile" envconfig:"K8S_CERT_FILE" default:""`
K8sCAFile string `json:"k8sCAFile" envconfig:"K8S_CA_FILE" default:""`
K8sAPIToken string `json:"k8sAPIToken" ignore:"true"`
K8sInsecureSkipTLSVerify bool `json:"k8sInsecureSkipTLSVerify" envconfig:"K8S_INSECURE_SKIP_TLS_VERIFY" default:""`
K8sDisableNodePoll bool `json:"k8sDisableNodePoll" envconfig:"K8S_DISABLE_NODE_POLL" default:""`
// K8sUsePodCIDR controls whether or not IPAM blocks are generated based on Node.Spec.PodCIDR. Set this
// to true when using host-local IPAM, and set to false when using calico-ipam.
K8sUsePodCIDR bool `json:"usePodCIDR" envconfig:"USE_POD_CIDR" default:""`
}
// NewCalicoAPIConfig creates a new (zeroed) CalicoAPIConfig struct with the
// TypeMetadata initialised to the current version.
func NewCalicoAPIConfig() *CalicoAPIConfig {
return &CalicoAPIConfig{
TypeMeta: metav1.TypeMeta{
Kind: KindCalicoAPIConfig,
APIVersion: apiv3.GroupVersionCurrent,
},
}
}
// IsAlphaFeatureSet checks if the comma separated features have the
// name set in it.
func IsAlphaFeatureSet(features, name string) bool {
fs := strings.Split(features, ",")
for _, f := range fs {
if f == name {
return true
}
}
return false
}

View File

@@ -0,0 +1,76 @@
package apiconfig
import (
"errors"
"fmt"
"io/ioutil"
"github.com/kelseyhightower/envconfig"
yaml "github.com/projectcalico/go-yaml-wrapper"
apiv3 "github.com/projectcalico/libcalico-go/lib/apis/v3"
log "github.com/sirupsen/logrus"
)
// LoadClientConfig loads the ClientConfig from the specified file (if specified)
// or from environment variables (if the file is not specified).
func LoadClientConfig(filename string) (*CalicoAPIConfig, error) {
// Override / merge with values loaded from the specified file.
if filename != "" {
b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c, err := LoadClientConfigFromBytes(b)
if err != nil {
return nil, fmt.Errorf("syntax error in %s: %v", filename, err)
}
return c, nil
}
return LoadClientConfigFromEnvironment()
}
// LoadClientConfig loads the ClientConfig from the supplied bytes containing
// YAML or JSON format data.
func LoadClientConfigFromBytes(b []byte) (*CalicoAPIConfig, error) {
var c CalicoAPIConfig
// Default the backend type to be etcd v3. This will be overridden if
// explicitly specified in the file.
log.Debug("Loading config from JSON or YAML data")
c = CalicoAPIConfig{
Spec: CalicoAPIConfigSpec{
DatastoreType: EtcdV3,
},
}
if err := yaml.UnmarshalStrict(b, &c); err != nil {
return nil, err
}
// Validate the version and kind.
if c.APIVersion != apiv3.GroupVersionCurrent {
return nil, errors.New("invalid config file: unknown APIVersion '" + c.APIVersion + "'")
}
if c.Kind != KindCalicoAPIConfig {
return nil, errors.New("invalid config file: expected kind '" + KindCalicoAPIConfig + "', got '" + c.Kind + "'")
}
log.Debug("Datastore type: ", c.Spec.DatastoreType)
return &c, nil
}
// LoadClientConfig loads the ClientConfig from the specified file (if specified)
// or from environment variables (if the file is not specified).
func LoadClientConfigFromEnvironment() (*CalicoAPIConfig, error) {
c := NewCalicoAPIConfig()
// Load client config from environment variables.
log.Debug("Loading config from environment")
if err := envconfig.Process("calico", &c.Spec); err != nil {
return nil, err
}
return c, nil
}