-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from wlan0/master
add support for binding and namespace
- Loading branch information
Showing
9 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package converters | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/koki/short/types" | ||
serrors "github.com/koki/structurederrors" | ||
) | ||
|
||
func Convert_Koki_Binding_to_Kube_Binding(kokiWrapper *types.BindingWrapper) (*v1.Binding, error) { | ||
var err error | ||
kubeBinding := &v1.Binding{} | ||
kokiBinding := kokiWrapper.Binding | ||
|
||
kubeBinding.Name = kokiBinding.Name | ||
kubeBinding.Namespace = kokiBinding.Namespace | ||
if len(kokiBinding.Version) == 0 { | ||
kubeBinding.APIVersion = "v1" | ||
} else { | ||
kubeBinding.APIVersion = kokiBinding.Version | ||
} | ||
kubeBinding.Kind = "Binding" | ||
kubeBinding.ClusterName = kokiBinding.Cluster | ||
kubeBinding.Labels = kokiBinding.Labels | ||
kubeBinding.Annotations = kokiBinding.Annotations | ||
|
||
target, err := revertTarget(&kokiBinding.Target) | ||
if err != nil { | ||
return nil, serrors.ContextualizeErrorf(err, "binding target") | ||
} | ||
kubeBinding.Target = *target | ||
|
||
return kubeBinding, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package converters | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/koki/short/types" | ||
serrors "github.com/koki/structurederrors" | ||
) | ||
|
||
func Convert_Koki_Namespace_to_Kube_Namespace(kokiWrapper *types.NamespaceWrapper) (*v1.Namespace, error) { | ||
var err error | ||
kubeNamespace := &v1.Namespace{} | ||
kokiNamespace := kokiWrapper.Namespace | ||
|
||
kubeNamespace.Name = kokiNamespace.Name | ||
kubeNamespace.Namespace = kokiNamespace.Namespace | ||
if len(kokiNamespace.Version) == 0 { | ||
kubeNamespace.APIVersion = "v1" | ||
} else { | ||
kubeNamespace.APIVersion = kokiNamespace.Version | ||
} | ||
kubeNamespace.Kind = "Namespace" | ||
kubeNamespace.ClusterName = kokiNamespace.Cluster | ||
kubeNamespace.Labels = kokiNamespace.Labels | ||
kubeNamespace.Annotations = kokiNamespace.Annotations | ||
|
||
spec, err := revertNamespaceSpec(kokiNamespace) | ||
if err != nil { | ||
return nil, serrors.ContextualizeErrorf(err, "Namespace spec") | ||
} | ||
kubeNamespace.Spec = spec | ||
|
||
status, err := revertNamespaceStatus(kokiNamespace) | ||
if err != nil { | ||
return nil, serrors.ContextualizeErrorf(err, "Namespace status") | ||
} | ||
kubeNamespace.Status = status | ||
|
||
return kubeNamespace, nil | ||
} | ||
|
||
func revertNamespaceStatus(kokiNamespace types.Namespace) (v1.NamespaceStatus, error) { | ||
var kubeStatus v1.NamespaceStatus | ||
|
||
if kokiNamespace.Phase == "" { | ||
return kubeStatus, nil | ||
} | ||
|
||
var phase v1.NamespacePhase | ||
switch kokiNamespace.Phase { | ||
case types.NamespaceActive: | ||
phase = v1.NamespaceActive | ||
case types.NamespaceTerminating: | ||
phase = v1.NamespaceTerminating | ||
default: | ||
return kubeStatus, serrors.InvalidValueErrorf(kokiNamespace.Phase, "Invalid namespace phase") | ||
} | ||
kubeStatus.Phase = phase | ||
|
||
return kubeStatus, nil | ||
} | ||
|
||
func revertNamespaceSpec(kokiNamespace types.Namespace) (v1.NamespaceSpec, error) { | ||
var kubeSpec v1.NamespaceSpec | ||
var kubeFinalizers []v1.FinalizerName | ||
|
||
for i := range kokiNamespace.Finalizers { | ||
kokiFinalizer := kokiNamespace.Finalizers[i] | ||
|
||
kubeFinalizer, err := revertFinalizer(kokiFinalizer) | ||
if err != nil { | ||
return kubeSpec, err | ||
} | ||
|
||
kubeFinalizers = append(kubeFinalizers, kubeFinalizer) | ||
} | ||
kubeSpec.Finalizers = kubeFinalizers | ||
|
||
return kubeSpec, nil | ||
} | ||
|
||
func revertFinalizer(kokiFinalizer types.FinalizerName) (v1.FinalizerName, error) { | ||
if kokiFinalizer == "" { | ||
return "", nil | ||
} | ||
|
||
switch kokiFinalizer { | ||
case types.FinalizerKubernetes: | ||
return v1.FinalizerKubernetes, nil | ||
} | ||
|
||
return "", serrors.InvalidValueErrorf(kokiFinalizer, "unrecognized value") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package converters | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/koki/short/types" | ||
serrors "github.com/koki/structurederrors" | ||
) | ||
|
||
func Convert_Kube_Binding_to_Koki_Binding(kubeBinding *v1.Binding) (*types.BindingWrapper, error) { | ||
kokiWrapper := &types.BindingWrapper{} | ||
kokiBinding := &kokiWrapper.Binding | ||
|
||
kokiBinding.Name = kubeBinding.Name | ||
kokiBinding.Namespace = kubeBinding.Namespace | ||
kokiBinding.Version = kubeBinding.APIVersion | ||
kokiBinding.Cluster = kubeBinding.ClusterName | ||
kokiBinding.Labels = kubeBinding.Labels | ||
kokiBinding.Annotations = kubeBinding.Annotations | ||
|
||
target, err := convertTarget(&kubeBinding.Target) | ||
if err != nil { | ||
return nil, serrors.ContextualizeErrorf(err, "Binding Target") | ||
} | ||
kokiBinding.Target = *target | ||
|
||
return kokiWrapper, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package converters | ||
|
||
import ( | ||
"k8s.io/api/core/v1" | ||
|
||
"github.com/koki/short/types" | ||
serrors "github.com/koki/structurederrors" | ||
) | ||
|
||
func Convert_Kube_Namespace_to_Koki_Namespace(kubeNamespace *v1.Namespace) (*types.NamespaceWrapper, error) { | ||
kokiWrapper := &types.NamespaceWrapper{} | ||
kokiNamespace := &kokiWrapper.Namespace | ||
|
||
kokiNamespace.Name = kubeNamespace.Name | ||
kokiNamespace.Namespace = kubeNamespace.Namespace | ||
kokiNamespace.Version = kubeNamespace.APIVersion | ||
kokiNamespace.Cluster = kubeNamespace.ClusterName | ||
kokiNamespace.Labels = kubeNamespace.Labels | ||
kokiNamespace.Annotations = kubeNamespace.Annotations | ||
|
||
finalizers, err := convertNamespaceSpec(kubeNamespace.Spec) | ||
if err != nil { | ||
return nil, err | ||
} | ||
kokiNamespace.Finalizers = finalizers | ||
|
||
phase, err := convertNamespaceStatus(kubeNamespace.Status) | ||
if err != nil { | ||
return nil, err | ||
} | ||
kokiNamespace.Phase = phase | ||
|
||
return kokiWrapper, nil | ||
} | ||
|
||
func convertNamespaceSpec(kubeSpec v1.NamespaceSpec) ([]types.FinalizerName, error) { | ||
var kokiFinalizers []types.FinalizerName | ||
|
||
for i := range kubeSpec.Finalizers { | ||
kubeFinalizer := kubeSpec.Finalizers[i] | ||
|
||
var kokiFinalizer types.FinalizerName | ||
switch kubeFinalizer { | ||
case v1.FinalizerKubernetes: | ||
kokiFinalizer = types.FinalizerKubernetes | ||
default: | ||
return nil, serrors.InvalidValueErrorf(kubeFinalizer, "unrecognized finalizer") | ||
} | ||
|
||
kokiFinalizers = append(kokiFinalizers, kokiFinalizer) | ||
} | ||
|
||
return kokiFinalizers, nil | ||
} | ||
|
||
func convertNamespaceStatus(kubeStatus v1.NamespaceStatus) (types.NamespacePhase, error) { | ||
switch kubeStatus.Phase { | ||
case v1.NamespaceActive: | ||
return types.NamespaceActive, nil | ||
case v1.NamespaceTerminating: | ||
return types.NamespaceTerminating, nil | ||
case "": | ||
return "", nil | ||
} | ||
|
||
return "", serrors.InvalidValueErrorf(kubeStatus.Phase, "invalid phase") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package types | ||
|
||
type BindingWrapper struct { | ||
Binding `json:"binding"` | ||
} | ||
|
||
type Binding struct { | ||
Version string `json:"version,omitempty"` | ||
Cluster string `json:"cluster,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Namespace string `json:"namespace,omitempty"` | ||
Labels map[string]string `json:"labels,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
|
||
Target ObjectReference `json:"target"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package types | ||
|
||
type NamespaceWrapper struct { | ||
Namespace `json:"namespace"` | ||
} | ||
|
||
type Namespace struct { | ||
Version string `json:"version,omitempty"` | ||
Cluster string `json:"cluster,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Namespace string `json:"namespace,omitempty"` | ||
Labels map[string]string `json:"labels,omitempty"` | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
|
||
Finalizers []FinalizerName `json:"finalizers,omitempty"` | ||
|
||
Phase NamespacePhase `json:"phase,omitempty"` | ||
} | ||
|
||
type NamespacePhase string | ||
|
||
const ( | ||
NamespaceActive NamespacePhase = "Active" | ||
NamespaceTerminating NamespacePhase = "Terminating" | ||
) | ||
|
||
type FinalizerName string | ||
|
||
const ( | ||
FinalizerKubernetes FinalizerName = "kubernetes" | ||
) |