Skip to content

Commit

Permalink
Add error conversion from k8s errors -> libcalico errors
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed Dec 6, 2016
1 parent 93d0e15 commit 9f1acb4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/backend/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func NewKubeClient(kc *KubeConfig) (*KubeClient, error) {
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&loadingRules, configOverrides).ClientConfig()
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, nil)
}

// Create the clientset
cs, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, nil)
}
log.Debugf("Created k8s clientSet: %+v", cs)
return &KubeClient{clientSet: cs}, nil
Expand Down Expand Up @@ -187,7 +187,7 @@ func (c *KubeClient) listProfiles(l model.ProfileListOptions) ([]*model.KVPair,
// Otherwise, enumerate all.
namespaces, err := c.clientSet.Namespaces().List(k8sapi.ListOptions{})
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, l)
}

// For each Namespace, return a profile.
Expand All @@ -213,7 +213,7 @@ func (c *KubeClient) getProfile(k model.ProfileKey) (*model.KVPair, error) {
}
namespace, err := c.clientSet.Namespaces().Get(namespaceName)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, k)
}

return c.converter.namespaceToProfile(namespace)
Expand All @@ -228,13 +228,12 @@ func (c *KubeClient) applyWorkloadEndpoint(k *model.KVPair) (*model.KVPair, erro
ns, name := c.converter.parseWorkloadID(k.Key.(model.WorkloadEndpointKey).WorkloadID)
pod, err := c.clientSet.Pods(ns).Get(name)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, k.Key)
}
pod.Status.PodIP = ips[0].IP.String()
log.Debugf("Pod IP: %+v", pod.Status.PodIP)
pod, err = c.clientSet.Pods(ns).UpdateStatus(pod)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, k.Key)
}
log.Debugf("Successfully applied pod: %+v", pod)
return c.converter.podToWorkloadEndpoint(pod)
Expand Down Expand Up @@ -265,7 +264,7 @@ func (c *KubeClient) listWorkloadEndpoints(l model.WorkloadEndpointListOptions)
// We don't yet support hostname, orchestratorID, for the k8s backend.
pods, err := c.clientSet.Pods("").List(k8sapi.ListOptions{})
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, l)
}

// For each Pod, return a workload endpoint.
Expand Down Expand Up @@ -293,7 +292,7 @@ func (c *KubeClient) getWorkloadEndpoint(k model.WorkloadEndpointKey) (*model.KV

pod, err := c.clientSet.Pods(namespace).Get(podName)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, k)
}

// Decide if this pod should be displayed.
Expand Down Expand Up @@ -322,7 +321,7 @@ func (c *KubeClient) listPolicies(l model.PolicyListOptions) ([]*model.KVPair, e
Timeout(10 * time.Second).
Do().Into(&networkPolicies)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, l)
}

// For each policy, turn it into a Policy and generate the list.
Expand Down Expand Up @@ -354,7 +353,7 @@ func (c *KubeClient) getPolicy(k model.PolicyKey) (*model.KVPair, error) {
Timeout(10 * time.Second).
Do().Into(&networkPolicy)
if err != nil {
return nil, err
return nil, k8sErrorToCalico(err, k)
}
return c.converter.networkPolicyToPolicy(&networkPolicy)
}
Expand Down
47 changes: 47 additions & 0 deletions lib/backend/k8s/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2016 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 k8s

import (
"github.com/projectcalico/libcalico-go/lib/errors"

kerrors "k8s.io/client-go/pkg/api/errors"
)

// k8sErrorToCalico returns the equivalent libcalico error for the given
// kubernetes error.
func k8sErrorToCalico(ke error, id interface{}) error {
if kerrors.IsAlreadyExists(ke) {
return errors.ErrorResourceAlreadyExists{
Err: ke,
Identifier: id,
}
}
if kerrors.IsNotFound(ke) {
return errors.ErrorResourceDoesNotExist{
Err: ke,
Identifier: id,
}
}
if kerrors.IsForbidden(ke) || kerrors.IsUnauthorized(ke) {
return errors.ErrorConnectionUnauthorized{
Err: ke,
}
}
return errors.ErrorDatastoreError{
Err: ke,
Identifier: id,
}
}

0 comments on commit 9f1acb4

Please sign in to comment.