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

[release-4.18] OCPBUGS-46411: skip generating certs when networkConfig.status.ServiceNetwork is nil #1780

Open
wants to merge 2 commits into
base: release-4.18
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions pkg/operator/certrotationcontroller/servicehostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func (c *CertRotationController) syncServiceHostnames() error {
if err != nil {
return err
}

// skip when the status.serviceNetwork is nil
if len(networkConfig.Status.ServiceNetwork) == 0 {
return fmt.Errorf("empty networkConfig ServiceNetwork, can't generate cert")
}

for _, cidrString := range networkConfig.Status.ServiceNetwork {
_, serviceCIDR, err := net.ParseCIDR(cidrString)
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions pkg/operator/certrotationcontroller/servicehostname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package certrotationcontroller

import (
"fmt"
"testing"

configv1 "github.com/openshift/api/config/v1"
configv1listers "github.com/openshift/client-go/config/listers/config/v1"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)

func TestServiceHostNameFunc(t *testing.T) {
scenarios := []struct {
name string
objects []runtime.Object
expectedError error
}{
{
"network config status not available",
[]runtime.Object{
fakeNetwork(false),
},
fmt.Errorf("empty networkConfig ServiceNetwork, can't generate cert"),
},
{
"happy with network status network ServiceNetwork",
[]runtime.Object{
fakeNetwork(true),
},
nil,
},
}

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
indexer := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
for _, obj := range scenario.objects {
if err := indexer.Add(obj); err != nil {
require.NoError(t, err)
}
}
controller := CertRotationController{
networkLister: configv1listers.NewNetworkLister(indexer),
serviceNetwork: &DynamicServingRotation{hostnamesChanged: make(chan struct{}, 10)},
}
err := controller.syncServiceHostnames()
require.Equal(t, err, scenario.expectedError)
})
}
}

func fakeNetwork(hasServiceNetwork bool) *configv1.Network {
var serviceNetwork []string
if hasServiceNetwork {
serviceNetwork = []string{"10.0.1.0/24"}
} else {
serviceNetwork = []string{}
}
return &configv1.Network{
ObjectMeta: metav1.ObjectMeta{Name: "cluster"},
Status: configv1.NetworkStatus{ServiceNetwork: serviceNetwork},
}
}