Skip to content

Commit

Permalink
Merge pull request #283 from caseydavenport/cluster-guid
Browse files Browse the repository at this point in the history
k8s backend support for GlobalConfig
  • Loading branch information
caseydavenport authored Dec 6, 2016
2 parents 32e1bff + 00f19d4 commit 6163882
Show file tree
Hide file tree
Showing 13 changed files with 604 additions and 59 deletions.
18 changes: 8 additions & 10 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/backend/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type Client interface {
// the datastore and then generates subsequent KVPair updates for
// changes to the datastore.
Syncer(callbacks SyncerCallbacks) Syncer

// EnsureInitialized ensures that the backend is initialized
// any ready to be used.
EnsureInitialized() error
}

type Syncer interface {
Expand Down
4 changes: 4 additions & 0 deletions lib/backend/compat/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func NewAdaptor(c api.Client) *ModelAdaptor {
return &ModelAdaptor{client: c}
}

func (c *ModelAdaptor) EnsureInitialized() error {
return c.client.EnsureInitialized()
}

// Create an entry in the datastore. This errors if the entry already exists.
func (c *ModelAdaptor) Create(d *model.KVPair) (*model.KVPair, error) {
var err error
Expand Down
4 changes: 4 additions & 0 deletions lib/backend/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func NewEtcdClient(config *EtcdConfig) (*EtcdClient, error) {
return &EtcdClient{etcdClient: client, etcdKeysAPI: keys}, nil
}

func (c *EtcdClient) EnsureInitialized() error {
return nil
}

func (c *EtcdClient) Syncer(callbacks api.SyncerCallbacks) api.Syncer {
return newSyncer(c.etcdKeysAPI, callbacks)
}
Expand Down
44 changes: 37 additions & 7 deletions lib/backend/k8s/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
"encoding/json"

log "github.com/Sirupsen/logrus"
"github.com/projectcalico/libcalico-go/lib/backend/k8s/thirdparty"
"github.com/projectcalico/libcalico-go/lib/backend/model"
cnet "github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
kapi "k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/unversioned"
k8sapi "k8s.io/client-go/pkg/api/v1"
kapiv1 "k8s.io/client-go/pkg/api/v1"
extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1"
)

Expand Down Expand Up @@ -79,7 +81,7 @@ func (c converter) parseProfileName(profileName string) (string, error) {
return splits[1], nil
}

func (c converter) namespaceToProfile(ns *k8sapi.Namespace) (*model.KVPair, error) {
func (c converter) namespaceToProfile(ns *kapiv1.Namespace) (*model.KVPair, error) {
// Determine the ingress action based off the DefaultDeny annotation.
ingressAction := "allow"
for k, v := range ns.ObjectMeta.Annotations {
Expand Down Expand Up @@ -116,21 +118,49 @@ func (c converter) namespaceToProfile(ns *k8sapi.Namespace) (*model.KVPair, erro
return &kvp, nil
}

func (c converter) tprToGlobalConfig(tpr *thirdparty.GlobalConfig) *model.KVPair {
kvp := &model.KVPair{
Key: model.GlobalConfigKey{
Name: tpr.Spec.Name,
},
Value: tpr.Spec.Value,
Revision: tpr.Metadata.ResourceVersion,
}
return kvp
}

func (c converter) globalConfigToTPR(kvp *model.KVPair) thirdparty.GlobalConfig {
tpr := thirdparty.GlobalConfig{
Metadata: kapi.ObjectMeta{
// Names in Kubernetes must be lower-case.
Name: strings.ToLower(kvp.Key.(model.GlobalConfigKey).Name),
},
Spec: thirdparty.GlobalConfigSpec{
Name: kvp.Key.(model.GlobalConfigKey).Name,
Value: kvp.Value.(string),
},
}
if kvp.Revision != nil {
tpr.Metadata.ResourceVersion = kvp.Revision.(string)
}
return tpr
}

// isCalicoPod returns true if the pod should be shown as a workloadEndpoint
// in the Calico API and false otherwise.
func (c converter) isCalicoPod(pod *k8sapi.Pod) bool {
func (c converter) isCalicoPod(pod *kapiv1.Pod) bool {
return !c.isHostNetworked(pod) && c.hasIPAddress(pod)
}

func (c converter) isHostNetworked(pod *k8sapi.Pod) bool {
func (c converter) isHostNetworked(pod *kapiv1.Pod) bool {
return pod.Spec.HostNetwork
}

func (c converter) hasIPAddress(pod *k8sapi.Pod) bool {
func (c converter) hasIPAddress(pod *kapiv1.Pod) bool {
return pod.Status.PodIP != ""
}

func (c converter) podToWorkloadEndpoint(pod *k8sapi.Pod) (*model.KVPair, error) {
func (c converter) podToWorkloadEndpoint(pod *kapiv1.Pod) (*model.KVPair, error) {
// Pull out the profile and workload ID based on pod name and Namespace.
profile := fmt.Sprintf("k8s_ns.%s", pod.ObjectMeta.Namespace)
workload := fmt.Sprintf("%s.%s", pod.ObjectMeta.Namespace, pod.ObjectMeta.Name)
Expand Down Expand Up @@ -297,7 +327,7 @@ func (c converter) buildRule(port *extensions.NetworkPolicyPort, peer *extension
}
}

func (c converter) k8sProtocolToCalico(protocol *k8sapi.Protocol) *numorstring.Protocol {
func (c converter) k8sProtocolToCalico(protocol *kapiv1.Protocol) *numorstring.Protocol {
if protocol != nil {
p := numorstring.ProtocolFromString(strings.ToLower(string(*protocol)))
return &p
Expand Down
Loading

0 comments on commit 6163882

Please sign in to comment.