Skip to content

Commit

Permalink
Validate nodeset networks order
Browse files Browse the repository at this point in the history
closes OSPRH-9455

Signed-off-by: Fabricio Aguiar <[email protected]>
  • Loading branch information
fao89 committed Aug 28, 2024
1 parent 8bc923a commit 5a60919
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
22 changes: 22 additions & 0 deletions apis/dataplane/v1beta1/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright 2023.
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 v1beta1

const (
// CtlPlaneNetwork - default ctlplane Network Name in NetConfig
CtlPlaneNetwork = "ctlplane"
)
26 changes: 26 additions & 0 deletions apis/dataplane/v1beta1/openstackdataplanenodeset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,29 @@ func (r *OpenStackDataPlaneNodeSetSpec) TLSMatch(controlPlane openstackv1.OpenSt
}
return nil
}

// Validate NodeSet networks
func (r *OpenStackDataPlaneNodeSetSpec) ValidateNetworks() (errors field.ErrorList) {
for _, node := range r.Nodes {
if len(node.Networks) > 0 && node.Networks[0].Name != CtlPlaneNetwork {
errors = append(errors, field.Invalid(
field.NewPath("spec").Child("nodes"),
node.Networks,
fmt.Sprintf(
"node %s error: networks should start with %s got %s instead",
node.HostName, CtlPlaneNetwork, node.Networks[0].Name,
)))
}
}
if len(r.NodeTemplate.Networks) > 0 && r.NodeTemplate.Networks[0].Name != CtlPlaneNetwork {
errors = append(errors, field.Invalid(
field.NewPath("spec").Child("nodeTemplate"),
r.NodeTemplate.Networks,
fmt.Sprintf(
"networks should start with %s got %s instead",
CtlPlaneNetwork, r.NodeTemplate.Networks[0].Name,
)))
}

return errors
}
2 changes: 2 additions & 0 deletions apis/dataplane/v1beta1/openstackdataplanenodeset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func (r *OpenStackDataPlaneNodeSet) ValidateCreate() (admission.Warnings, error)
if err != nil {
return nil, err
}
errors = append(errors, r.Spec.ValidateNetworks()...)

// Check if OpenStackDataPlaneNodeSet name matches RFC1123 for use in labels
validate := validator.New()
Expand Down Expand Up @@ -184,6 +185,7 @@ func (r *OpenStackDataPlaneNodeSet) ValidateUpdate(old runtime.Object) (admissio
return nil, err
}

errors = append(errors, r.Spec.ValidateNetworks()...)
errors = append(errors, r.Spec.ValidateUpdate(&oldNodeSet.Spec)...)

if errors != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/dataplane/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ func DeployBaremetalSet(
instanceSpec.CtlPlaneIP = fmt.Sprintf("%s/24", node.Ansible.AnsibleHost)
} else {
for _, res := range ipSet.Status.Reservation {
if strings.ToLower(string(res.Network)) == CtlPlaneNetwork {
if strings.ToLower(string(res.Network)) == dataplanev1.CtlPlaneNetwork {
_, ipNet, err := net.ParseCIDR(res.Cidr)
if err != nil {
return err
}
ipPrefix, _ := ipNet.Mask.Size()
instanceSpec.CtlPlaneIP = fmt.Sprintf("%s/%d", res.Address, ipPrefix)
if res.Gateway == nil {
return fmt.Errorf("%s gateway is missing", CtlPlaneNetwork)
return fmt.Errorf("%s gateway is missing", dataplanev1.CtlPlaneNetwork)
}
baremetalSet.Spec.CtlplaneGateway = *res.Gateway
baremetalSet.Spec.BootstrapDNS = dnsAddresses
Expand Down
2 changes: 1 addition & 1 deletion pkg/dataplane/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func EnsureTLSCerts(ctx context.Context, helper *helper.Helper,
// NOTE: we are assuming that there will always be a ctlplane network
// that means if you are not using network isolation with multiple networks
// you should still need to have a ctlplane network at a minimum to use tls-e
baseName, ok := dnsNames[CtlPlaneNetwork]
baseName, ok := dnsNames[dataplanev1.CtlPlaneNetwork]
if !ok {
return &result, fmt.Errorf(
"control plane network not found for node %s , tls-e requires a control plane network to be present",
Expand Down
4 changes: 0 additions & 4 deletions pkg/dataplane/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ limitations under the License.
package deployment

const (

// CtlPlaneNetwork - default ctlplane Network Name in NetConfig
CtlPlaneNetwork = "ctlplane"

// ValidateNetworkLabel for ValidateNetwork OpenStackAnsibleEE
ValidateNetworkLabel = "validate-network"

Expand Down
6 changes: 3 additions & 3 deletions pkg/dataplane/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ func populateInventoryFromIPAM(
netCidr, _ := ipnet.Mask.Size()
host.Vars[entry+"_cidr"] = netCidr
}
if res.Vlan != nil || entry != CtlPlaneNetwork {
if res.Vlan != nil || entry != dataplanev1.CtlPlaneNetwork {
host.Vars[entry+"_vlan_id"] = res.Vlan
}
host.Vars[entry+"_mtu"] = res.MTU
host.Vars[entry+"_gateway_ip"] = res.Gateway
host.Vars[entry+"_host_routes"] = res.Routes

if entry == CtlPlaneNetwork {
if entry == dataplanev1.CtlPlaneNetwork {
host.Vars[entry+"_dns_nameservers"] = dnsAddresses
if dataplanev1.NodeHostNameIsFQDN(hostName) {
host.Vars["canonical_hostname"] = hostName
Expand Down Expand Up @@ -355,7 +355,7 @@ func buildNetworkVars(networks []infranetworkv1.IPSetNetwork) ([]string, map[str
var nets []string
for _, network := range networks {
netName := string(network.Name)
if strings.EqualFold(netName, CtlPlaneNetwork) {
if strings.EqualFold(netName, dataplanev1.CtlPlaneNetwork) {
continue
}
nets = append(nets, netName)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dataplane/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func createOrPatchDNSData(ctx context.Context, helper *helper.Helper,
fqdnNames = append(fqdnNames, fqdnName)
dnsDetails.Hostnames[hostName][infranetworkv1.NetNameStr(netLower)] = fqdnName
}
if dataplanev1.NodeHostNameIsFQDN(hostName) && netLower == CtlPlaneNetwork {
if dataplanev1.NodeHostNameIsFQDN(hostName) && netLower == dataplanev1.CtlPlaneNetwork {
fqdnNames = append(fqdnNames, hostName)
dnsDetails.Hostnames[hostName][infranetworkv1.NetNameStr(netLower)] = hostName
}
Expand All @@ -137,7 +137,7 @@ func createOrPatchDNSData(ctx context.Context, helper *helper.Helper,
allDNSRecords = append(allDNSRecords, dnsRecord)
// Adding only ctlplane domain for ansibleee.
// TODO (rabi) This is not very efficient.
if netLower == CtlPlaneNetwork && ctlplaneSearchDomain == "" {
if netLower == dataplanev1.CtlPlaneNetwork && ctlplaneSearchDomain == "" {
dnsDetails.CtlplaneSearchDomain = res.DNSDomain
}
}
Expand Down

0 comments on commit 5a60919

Please sign in to comment.