2019-08-24 16:52:42 +07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright 2019 The Rook Authors. 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 k8sutil
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
|
"fmt"
|
2021-06-21 14:40:01 -06:00
|
|
|
|
"sort"
|
2019-08-24 16:52:42 +07:00
|
|
|
|
"strings"
|
|
|
|
|
|
|
2020-04-01 09:58:04 +02:00
|
|
|
|
netapi "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
|
2021-05-14 18:03:38 -06:00
|
|
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
2019-08-24 16:52:42 +07:00
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2020-06-30 23:08:44 +05:30
|
|
|
|
const (
|
|
|
|
|
|
// publicNetworkSelectorKeyName is the network selector key for the ceph public network
|
|
|
|
|
|
publicNetworkSelectorKeyName = "public"
|
2021-04-07 02:37:37 +05:30
|
|
|
|
// clusterNetworkSelectorKeyName is the network selector key for the ceph cluster network
|
|
|
|
|
|
clusterNetworkSelectorKeyName = "cluster"
|
2020-06-30 23:08:44 +05:30
|
|
|
|
)
|
|
|
|
|
|
|
2020-04-01 09:58:04 +02:00
|
|
|
|
// NetworkAttachmentConfig represents the configuration of the NetworkAttachmentDefinitions object
|
|
|
|
|
|
type NetworkAttachmentConfig struct {
|
2021-03-25 16:26:02 +01:00
|
|
|
|
CniVersion string `json:"cniVersion,omitempty"`
|
|
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
|
|
Master string `json:"master,omitempty"`
|
|
|
|
|
|
Mode string `json:"mode,omitempty"`
|
2020-04-01 09:58:04 +02:00
|
|
|
|
Ipam struct {
|
2021-03-25 16:26:02 +01:00
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
|
|
Subnet string `json:"subnet,omitempty"`
|
|
|
|
|
|
Addresses []struct {
|
|
|
|
|
|
Address string `json:"address,omitempty"`
|
|
|
|
|
|
Gateway string `json:"gateway,omitempty"`
|
|
|
|
|
|
} `json:"addresses,omitempty"`
|
|
|
|
|
|
Ranges [][]struct {
|
|
|
|
|
|
Subnet string `json:"subnet,omitempty"`
|
|
|
|
|
|
RangeStart string `json:"rangeStart,omitempty"`
|
|
|
|
|
|
RangeEnd string `json:"rangeEnd,omitempty"`
|
|
|
|
|
|
Gateway string `json:"gateway,omitempty"`
|
|
|
|
|
|
} `json:"ranges,omitempty"`
|
|
|
|
|
|
Range string `json:"range,omitempty"`
|
|
|
|
|
|
RangeStart string `json:"rangeStart,omitempty"`
|
|
|
|
|
|
RangeEnd string `json:"rangeEnd,omitempty"`
|
2020-04-01 09:58:04 +02:00
|
|
|
|
Routes []struct {
|
2021-03-25 16:26:02 +01:00
|
|
|
|
Dst string `json:"dst,omitempty"`
|
|
|
|
|
|
} `json:"routes,omitempty"`
|
|
|
|
|
|
Gateway string `json:"gateway,omitempty"`
|
|
|
|
|
|
} `json:"ipam,omitempty"`
|
2020-04-01 09:58:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-08-24 16:52:42 +07:00
|
|
|
|
// ApplyMultus apply multus selector to Pods
|
|
|
|
|
|
// Multus supports short and json syntax, use only one kind at a time.
|
2021-05-14 18:03:38 -06:00
|
|
|
|
func ApplyMultus(net cephv1.NetworkSpec, objectMeta *metav1.ObjectMeta) error {
|
2019-08-24 16:52:42 +07:00
|
|
|
|
v := make([]string, 0, 2)
|
|
|
|
|
|
shortSyntax := false
|
|
|
|
|
|
jsonSyntax := false
|
|
|
|
|
|
|
2020-06-30 23:08:44 +05:30
|
|
|
|
for k, ns := range net.Selectors {
|
2019-08-24 16:52:42 +07:00
|
|
|
|
var multusMap map[string]string
|
|
|
|
|
|
err := json.Unmarshal([]byte(ns), &multusMap)
|
|
|
|
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
jsonSyntax = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
shortSyntax = true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-21 16:28:14 -06:00
|
|
|
|
app, ok := objectMeta.Labels["app"]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
app = "" // unknown app
|
2021-04-07 02:37:37 +05:30
|
|
|
|
}
|
2021-06-21 16:28:14 -06:00
|
|
|
|
isClusterNetApp := false
|
|
|
|
|
|
for _, clusterNetworkApp := range getClusterNetworkApps() {
|
|
|
|
|
|
if app == clusterNetworkApp {
|
|
|
|
|
|
isClusterNetApp = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if isClusterNetApp {
|
|
|
|
|
|
// append all networks to apps that are cluster network apps
|
2021-04-07 02:37:37 +05:30
|
|
|
|
v = append(v, string(ns))
|
|
|
|
|
|
} else {
|
2021-06-21 16:28:14 -06:00
|
|
|
|
// only append public networks to apps that are not cluster network apps
|
2020-06-30 23:08:44 +05:30
|
|
|
|
if k == publicNetworkSelectorKeyName {
|
|
|
|
|
|
v = append(v, string(ns))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-08-24 16:52:42 +07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if shortSyntax && jsonSyntax {
|
|
|
|
|
|
return fmt.Errorf("ApplyMultus: Can't mix short and JSON form")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-21 14:40:01 -06:00
|
|
|
|
// Sort network strings so that pods/deployments won't need updated in a loop if nothing changes
|
|
|
|
|
|
sort.Strings(v)
|
|
|
|
|
|
|
2019-08-24 16:52:42 +07:00
|
|
|
|
networks := strings.Join(v, ", ")
|
|
|
|
|
|
if jsonSyntax {
|
|
|
|
|
|
networks = "[" + networks + "]"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-22 16:39:09 -06:00
|
|
|
|
t := cephv1.Annotations{
|
2019-08-24 16:52:42 +07:00
|
|
|
|
"k8s.v1.cni.cncf.io/networks": networks,
|
|
|
|
|
|
}
|
|
|
|
|
|
t.ApplyToObjectMeta(objectMeta)
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2020-04-01 09:58:04 +02:00
|
|
|
|
|
2021-04-07 02:37:37 +05:30
|
|
|
|
// getClusterNetworkApps returns the list of ceph apps that utilize cluster network
|
|
|
|
|
|
func getClusterNetworkApps() []string {
|
2021-06-21 16:28:14 -06:00
|
|
|
|
return []string{"rook-ceph-osd"}
|
2021-04-07 02:37:37 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
2020-04-01 09:58:04 +02:00
|
|
|
|
// GetNetworkAttachmentConfig returns the NetworkAttachmentDefinitions configuration
|
|
|
|
|
|
func GetNetworkAttachmentConfig(n netapi.NetworkAttachmentDefinition) (NetworkAttachmentConfig, error) {
|
|
|
|
|
|
netConfigJSON := n.Spec.Config
|
|
|
|
|
|
var netConfig NetworkAttachmentConfig
|
|
|
|
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(netConfigJSON), &netConfig)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return netConfig, fmt.Errorf("failed to unmarshal netconfig json %q. %v", netConfigJSON, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return netConfig, nil
|
|
|
|
|
|
}
|