-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #4124 from EdDev/tests-libnet
tests, libnet: Move helpers into libnet
- Loading branch information
Showing
12 changed files
with
205 additions
and
45 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
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,104 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libnet | ||
|
||
import ( | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type CloudInitNetworkData struct { | ||
Version int `yaml:"version"` | ||
Ethernets map[string]CloudInitInterface `yaml:"ethernets,omitempty"` | ||
} | ||
|
||
type CloudInitInterface struct { | ||
AcceptRA *bool `yaml:"accept-ra,omitempty"` | ||
Addresses []string `yaml:"addresses,omitempty"` | ||
DHCP4 *bool `yaml:"dhcp4,omitempty"` | ||
DHCP6 *bool `yaml:"dhcp6,omitempty"` | ||
DHCPIdentifier string `yaml:"dhcp-identifier,omitempty"` // "duid" or "mac" | ||
Gateway4 string `yaml:"gateway4,omitempty"` | ||
Gateway6 string `yaml:"gateway6,omitempty"` | ||
Nameservers CloudInitNameservers `yaml:"nameservers,omitempty"` | ||
MACAddress string `yaml:"macaddress,omitempty"` | ||
MTU int `yaml:"mtu,omitempty"` | ||
Routes []CloudInitRoute `yaml:"routes,omitempty"` | ||
} | ||
|
||
type CloudInitNameservers struct { | ||
Search []string `yaml:"search,omitempty,flow"` | ||
Addresses []string `yaml:"addresses,omitempty,flow"` | ||
} | ||
|
||
type CloudInitRoute struct { | ||
From string `yaml:"from,omitempty"` | ||
OnLink *bool `yaml:"on-link,omitempty"` | ||
Scope string `yaml:"scope,omitempty"` | ||
Table *int `yaml:"table,omitempty"` | ||
To string `yaml:"to,omitempty"` | ||
Type string `yaml:"type,omitempty"` | ||
Via string `yaml:"via,omitempty"` | ||
Metric *int `yaml:"metric,omitempty"` | ||
} | ||
|
||
const ( | ||
ipv6MasqueradeAddress = "fd10:0:2::2/120" | ||
ipv6MasqueradeGateway = "fd10:0:2::1" | ||
) | ||
|
||
// CreateDefaultCloudInitNetworkData generates a default configuration | ||
// for the Cloud-Init Network Data, in version 2 format. | ||
// The default configuration sets dynamic IPv4 (DHCP) and static IPv6 addresses, | ||
// inclusing DNS settings of the cluster nameserver IP and search domains. | ||
func CreateDefaultCloudInitNetworkData() (string, error) { | ||
dnsServerIP, err := ClusterDNSServiceIP() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
enabled := true | ||
networkData, err := CreateCloudInitNetworkData( | ||
&CloudInitNetworkData{ | ||
Version: 2, | ||
Ethernets: map[string]CloudInitInterface{ | ||
"eth0": { | ||
Addresses: []string{ipv6MasqueradeAddress}, | ||
DHCP4: &enabled, | ||
Gateway6: ipv6MasqueradeGateway, | ||
Nameservers: CloudInitNameservers{ | ||
Addresses: []string{dnsServerIP}, | ||
Search: SearchDomains(), | ||
}, | ||
}, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(networkData), nil | ||
} | ||
|
||
// CreateCloudInitNetworkData generates a configuration for the Cloud-Init Network Data version 2 format | ||
// based on the inputed data. | ||
func CreateCloudInitNetworkData(networkData *CloudInitNetworkData) ([]byte, error) { | ||
return yaml.Marshal(networkData) | ||
} |
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,60 @@ | ||
/* | ||
* This file is part of the KubeVirt project | ||
* | ||
* 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. | ||
* | ||
* Copyright 2020 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package libnet | ||
|
||
import ( | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"kubevirt.io/client-go/kubecli" | ||
) | ||
|
||
const ( | ||
k8sDNSServiceName = "kube-dns" | ||
k8sDNSNamespace = "kube-system" | ||
|
||
openshiftDNSServiceName = "dns-default" | ||
openshiftDNSNamespace = "openshift-dns" | ||
) | ||
|
||
// SearchDomains returns a list of default search name domains. | ||
func SearchDomains() []string { | ||
return []string{"default.svc.cluster.local", "svc.cluster.local", "cluster.local"} | ||
} | ||
|
||
// ClusterDNSServiceIP returns the cluster IP address of the DNS service. | ||
// Attempts first to detect the DNS service on a k8s cluster and if not found on an openshift cluster. | ||
func ClusterDNSServiceIP() (string, error) { | ||
virtClient, err := kubecli.GetKubevirtClient() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
service, err := virtClient.CoreV1().Services(k8sDNSNamespace).Get(k8sDNSServiceName, metav1.GetOptions{}) | ||
if err != nil { | ||
prevErr := err | ||
service, err = virtClient.CoreV1().Services(openshiftDNSNamespace).Get(openshiftDNSServiceName, metav1.GetOptions{}) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to detect the DNS services: %v, %v", prevErr, err) | ||
} | ||
} | ||
return service.Spec.ClusterIP, 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 |
---|---|---|
|
@@ -17,7 +17,7 @@ | |
* | ||
*/ | ||
|
||
package network | ||
package libnet | ||
|
||
import ( | ||
"fmt" | ||
|
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
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