Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DANM 4.0 EP5: Adapting netwatcher to create VLAN and VxLAN interfaces for all APIs #97

Merged
merged 2 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions cmd/netwatcher/netwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/cache"
"github.com/nokia/danm/pkg/netcontrol"
)

Expand All @@ -17,11 +16,6 @@ func getClientConfig(kubeConfig *string) (*rest.Config, error) {
return rest.InClusterConfig()
}

func watchRes(controller cache.Controller) {
stop := make(chan struct{})
go controller.Run(stop)
}

func main() {
log.SetOutput(os.Stdout)
log.Println("Starting DANM Watcher...")
Expand All @@ -32,14 +26,12 @@ func main() {
log.Println("ERROR: Parsing kubeconfig failed with error:" + err.Error() + " , exiting")
os.Exit(-1)
}
netHandler, err := netcontrol.NewHandler(config)
netWatcher, err := netcontrol.NewWatcher(config)
if err != nil {
log.Println("ERROR: Creation of K8s DanmNet Controller failed with error:" + err.Error() + " , exiting")
log.Println("ERROR: Creation of NetWatcher failed with error:" + err.Error() + " , exiting")
os.Exit(-1)
}
dnController := netHandler.CreateController()
watchRes(dnController)

// Wait forever
stopCh := make(chan struct{})
netWatcher.Run(&stopCh)
select {}
}
4 changes: 4 additions & 0 deletions crd/apis/danm/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&DanmEpList{},
&DanmNet{},
&DanmNetList{},
&ClusterNetwork{},
&ClusterNetworkList{},
&TenantNetwork{},
&TenantNetworkList{},
)
meta_v1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
Expand Down
46 changes: 41 additions & 5 deletions crd/apis/danm/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ type DanmNetOption struct {
Vlan int `json:"vlan,omitempty"`
}

type IP4Pool struct {
Start string `json:"start"`
End string `json:"end"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DanmNetList struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ListMeta `json:"metadata"`
Items []DanmNet `json:"items"`
}

type IP4Pool struct {
Start string `json:"start"`
End string `json:"end"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type DanmEp struct {
Expand Down Expand Up @@ -95,6 +95,7 @@ type DanmEpList struct {
meta_v1.ListMeta `json:"metadata"`
Items []DanmEp `json:"items"`
}

// VERY IMPORTANT NOT TO CHANGE THIS, INCLUDING THE EMPTY LINE BETWEEN THE ANNOTATIONS!!!
// https://github.com/kubernetes/code-generator/issues/59
// +genclient:nonNamespaced
Expand All @@ -121,4 +122,39 @@ type TenantConfigList struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ListMeta `json:"metadata"`
Items []TenantConfig `json:"items"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type TenantNetwork struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ObjectMeta `json:"metadata"`
Spec DanmNetSpec `json:"spec"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type TenantNetworkList struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ListMeta `json:"metadata"`
Items []TenantNetwork `json:"items"`
}

// VERY IMPORTANT NOT TO CHANGE THIS, INCLUDING THE EMPTY LINE BETWEEN THE ANNOTATIONS!!!
// https://github.com/kubernetes/code-generator/issues/59
// +genclient:nonNamespaced

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +genclient
type ClusterNetwork struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ObjectMeta `json:"metadata"`
Spec DanmNetSpec `json:"spec"`
}

// +genclient:nonNamespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type ClusterNetworkList struct {
meta_v1.TypeMeta `json:",inline"`
meta_v1.ListMeta `json:"metadata"`
Items []ClusterNetwork `json:"items"`
}
10 changes: 10 additions & 0 deletions integration/crds/TenantNetwork.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ spec:
type: string
device_pool:
type: string
vxlan:
type: integer
format: int32
minimum: 1
maximum: 16777214
vlan:
type: integer
format: int32
minimum: 1
maximum: 4094
rt_tables:
type: integer
format: int32
Expand Down
2 changes: 2 additions & 0 deletions integration/manifests/netwatcher/0netwatcher_rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ rules:
- "danm.k8s.io"
resources:
- danmnets
- clusternetworks
- tenantnetworks
verbs:
- get
- list
Expand Down
23 changes: 7 additions & 16 deletions pkg/admit/netadmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"reflect"
"strings"
"strconv"
"time"
"encoding/json"
"math/rand"
Expand Down Expand Up @@ -217,33 +216,25 @@ func createPatchListFromNetChanges(origNetwork danmtypes.DanmNet, changedNetwork
patchList := make([]Patch, 0)
if origNetwork.Spec.Options.Alloc != changedNetwork.Spec.Options.Alloc {
//TODO: Could (?) use some reflecting here to determine name of the struct field
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Alloc"],
json.RawMessage(`"` + changedNetwork.Spec.Options.Alloc + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Alloc"], changedNetwork.Spec.Options.Alloc))
}
if origNetwork.Spec.NetworkType != changedNetwork.Spec.NetworkType {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["NetworkType"],
json.RawMessage(`"` + changedNetwork.Spec.NetworkType + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["NetworkType"], changedNetwork.Spec.NetworkType))
}
if origNetwork.Spec.NetworkID != changedNetwork.Spec.NetworkID {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["NetworkID"],
json.RawMessage(`"` + changedNetwork.Spec.NetworkID + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["NetworkID"], changedNetwork.Spec.NetworkID))
}
if !reflect.DeepEqual(origNetwork.Spec.Options.Pool, changedNetwork.Spec.Options.Pool) {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Pool"],
json.RawMessage(`{"Start":"` + changedNetwork.Spec.Options.Pool.Start +
`","End":"` + changedNetwork.Spec.Options.Pool.End + `"}`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Pool"], changedNetwork.Spec.Options.Pool))
}
if origNetwork.Spec.Options.Device != changedNetwork.Spec.Options.Device {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Device"],
json.RawMessage(`"` + changedNetwork.Spec.Options.Device + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Device"], changedNetwork.Spec.Options.Device))
}
if origNetwork.Spec.Options.Vlan != changedNetwork.Spec.Options.Vlan {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Vlan"],
json.RawMessage(`"` + strconv.Itoa(changedNetwork.Spec.Options.Vlan) + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Vlan"], changedNetwork.Spec.Options.Vlan))
}
if origNetwork.Spec.Options.Vxlan != changedNetwork.Spec.Options.Vxlan {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Vxlan"],
json.RawMessage(`"` + strconv.Itoa(changedNetwork.Spec.Options.Vxlan) + `"`)))
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Vxlan"], changedNetwork.Spec.Options.Vxlan))
}
return patchList
}
6 changes: 3 additions & 3 deletions pkg/admit/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
type Patch struct {
Op string `json:"op"`
Path string `json:"path"`
Value json.RawMessage `json:"value"`
Value interface{} `json:"value,omitempty"`
}

func DecodeAdmissionReview(httpRequest *http.Request) (*v1beta1.AdmissionReview,error) {
Expand Down Expand Up @@ -77,14 +77,14 @@ func CreateReviewResponseFromPatches(patchList []Patch) *v1beta1.AdmissionRespon
}
}
if len(patches) > 0 {
reviewResponse.Patch = []byte(patches)
reviewResponse.Patch = patches
pt := v1beta1.PatchTypeJSONPatch
reviewResponse.PatchType = &pt
}
return &reviewResponse
}

func CreateGenericPatchFromChange(path string, value []byte ) Patch {
func CreateGenericPatchFromChange(path string, value interface{} ) Patch {
patch := Patch {
Op: "replace",
Path: path,
Expand Down
Loading