Skip to content

Commit

Permalink
MGMT-16759: Copy cluster proxy settings to local-cluster AgentCluster…
Browse files Browse the repository at this point in the history
…Install

The local cluster settings for HTTPProxy and HTTPSProxy should be copied to the AgentClusterInstall
This PR ensures that this takes place.
  • Loading branch information
paul-maidment committed Mar 5, 2024
1 parent ee0189b commit 4babfb8
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
6 changes: 6 additions & 0 deletions config/rbac/role.yaml
Expand Up @@ -294,6 +294,12 @@ rules:
- get
- list
- watch
- apiGroups:
- config.openshift.io
resources:
- proxies
verbs:
- get
- apiGroups:
- coordination.k8s.io
resources:
Expand Down
Expand Up @@ -673,6 +673,12 @@ spec:
- get
- list
- watch
- apiGroups:
- config.openshift.io
resources:
- proxies
verbs:
- get
- apiGroups:
- coordination.k8s.io
resources:
Expand Down
Expand Up @@ -201,6 +201,7 @@ type ComponentStatusFn func(context.Context, logrus.FieldLogger, string, appsv1.
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=clusterrolebindings,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=config.openshift.io,resources=proxies,verbs=get
// +kubebuilder:rbac:groups="apiregistration.k8s.io",resources=apiservices,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create

Expand Down
18 changes: 16 additions & 2 deletions pkg/localclusterimport/import_local_cluster.go
Expand Up @@ -85,7 +85,7 @@ func (i *LocalClusterImport) createLocalClusterPullSecret(sourceSecret *v1.Secre
return nil
}

func (i *LocalClusterImport) createAgentClusterInstall(numberOfControlPlaneNodes int) error {
func (i *LocalClusterImport) createAgentClusterInstall(numberOfControlPlaneNodes int, proxy *configv1.Proxy) error {
//Create an AgentClusterInstall in the local cluster namespace
userManagedNetworkingActive := true
agentClusterInstall := &hiveext.AgentClusterInstall{
Expand All @@ -106,6 +106,14 @@ func (i *LocalClusterImport) createAgentClusterInstall(numberOfControlPlaneNodes
}
agentClusterInstall.Namespace = i.localClusterNamespace
agentClusterInstall.Name = i.localClusterNamespace
if proxy != nil {
agentClusterInstall.Spec.Proxy = &hiveext.Proxy{
HTTPProxy: proxy.Spec.HTTPProxy,
HTTPSProxy: proxy.Spec.HTTPSProxy,
NoProxy: proxy.Spec.NoProxy,
}
}

err := i.clusterImportOperations.CreateAgentClusterInstall(agentClusterInstall)
if err != nil {
i.log.Errorf("could not create AgentClusterInstall due to error %s", err.Error())
Expand Down Expand Up @@ -222,6 +230,12 @@ func (i *LocalClusterImport) ImportLocalCluster() error {
errorList = multierror.Append(errorList, err)
}

proxy, err := i.clusterImportOperations.GetClusterProxy()
if err != nil {
i.log.Errorf("could not fetch proxy due to error %s", err.Error())
errorList = multierror.Append(errorList, err)
}

numberOfControlPlaneNodes, err := i.clusterImportOperations.GetNumberOfControlPlaneNodes()
if err != nil {
i.log.Errorf("unable to determine the number of control plane nodes due to error %s", err.Error())
Expand Down Expand Up @@ -266,7 +280,7 @@ func (i *LocalClusterImport) ImportLocalCluster() error {
}

if numberOfControlPlaneNodes > 0 {
err := i.createAgentClusterInstall(numberOfControlPlaneNodes)
err := i.createAgentClusterInstall(numberOfControlPlaneNodes, proxy)
if err != nil && !k8serrors.IsAlreadyExists(err) {
errorList = multierror.Append(errorList, err)
}
Expand Down
74 changes: 74 additions & 0 deletions pkg/localclusterimport/import_local_cluster_test.go
Expand Up @@ -36,6 +36,7 @@ var _ = Describe("ImportLocalCluster", func() {
agentServiceConfigUID types.UID
releaseImage string
localClusterNamespace string
clusterProxy *configv1.Proxy
)

BeforeEach(func() {
Expand Down Expand Up @@ -193,6 +194,26 @@ var _ = Describe("ImportLocalCluster", func() {
Return(dns, nil)
}

var mockGetClusterProxy = func(httpProxy string, httpsProxy string, noProxy string) *gomock.Call {
clusterProxy = &configv1.Proxy{
Spec: configv1.ProxySpec{
HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy,
NoProxy: noProxy,
},
}
return clusterImportOperations.EXPECT().
GetClusterProxy().
Return(clusterProxy, nil)
}

var mockNilClusterProxy = func() *gomock.Call {
clusterProxy = nil
return clusterImportOperations.EXPECT().
GetClusterProxy().
Return(clusterProxy, nil)
}

var mockNumberOfControlPlaneNodesFound = func() *gomock.Call {
return clusterImportOperations.EXPECT().
GetNumberOfControlPlaneNodes().
Expand Down Expand Up @@ -271,6 +292,13 @@ var _ = Describe("ImportLocalCluster", func() {
}
agentClusterInstall.Namespace = localClusterNamespace
agentClusterInstall.Name = localClusterNamespace
if clusterProxy != nil {
agentClusterInstall.Spec.Proxy = &hiveext.Proxy{
HTTPProxy: clusterProxy.Spec.HTTPProxy,
HTTPSProxy: clusterProxy.Spec.HTTPSProxy,
NoProxy: clusterProxy.Spec.NoProxy,
}
}
return agentClusterInstall
}

Expand Down Expand Up @@ -434,6 +462,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsNotFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
result := localClusterImport.ImportLocalCluster()
multiErrors := result.(*multierror.Error)
Expand All @@ -448,6 +477,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretNotFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
result := localClusterImport.ImportLocalCluster()
multiErrors := result.(*multierror.Error)
Expand All @@ -462,6 +492,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSNotFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
result := localClusterImport.ImportLocalCluster()
multiErrors := result.(*multierror.Error)
Expand All @@ -476,6 +507,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockFailedToCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
Expand All @@ -496,6 +528,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockFailedToCreateLocalClusterAdminKubeConfig()
Expand All @@ -516,6 +549,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
Expand All @@ -536,6 +570,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
Expand All @@ -556,6 +591,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
Expand All @@ -570,12 +606,49 @@ var _ = Describe("ImportLocalCluster", func() {
Expect(apiStatus.Status().Message).To(Equal("Invalid parameters"))
})

It("should copy cluster proxy entries to AgentClusterInstall", func() {
mockClusterVersionFound()
mockCreateNamespace()
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("http://http_proxy.net", "https://https_proxy.net", "localhost")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
mockCreateLocalClusterPullSecret()
mockCreateAgentClusterInstall()
mockGetAgentServiceConfig()
mockCreateLocalClusterDeployment()
result := localClusterImport.ImportLocalCluster()
Expect(result).To(BeNil())
})

It("should skip proxy creation gracefully if Cluster Proxy is nil", func() {
mockClusterVersionFound()
mockCreateNamespace()
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockNilClusterProxy()
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
mockCreateLocalClusterPullSecret()
mockCreateAgentClusterInstall()
mockGetAgentServiceConfig()
mockCreateLocalClusterDeployment()
result := localClusterImport.ImportLocalCluster()
Expect(result).To(BeNil())
})

It("should have created all entities required to import local cluster", func() {
mockClusterVersionFound()
mockCreateNamespace()
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockCreateClusterImageSet()
mockCreateLocalClusterAdminKubeConfig()
Expand All @@ -592,6 +665,7 @@ var _ = Describe("ImportLocalCluster", func() {
mockNodeKubeConfigsFound()
mockLocalClusterPullSecretFound()
mockClusterDNSFound()
mockGetClusterProxy("", "", "")
mockNumberOfControlPlaneNodesFound()
mockAlreadyExistsOnCreateNamespace()
mockAlreadyExistsOnCreateClusterImageSet()
Expand Down
14 changes: 14 additions & 0 deletions pkg/localclusterimport/local_cluster_import_operations.go
Expand Up @@ -24,6 +24,7 @@ type ClusterImportOperations interface {
GetNodes() (*v1.NodeList, error)
GetNumberOfControlPlaneNodes() (int, error)
GetClusterDNS() (*configv1.DNS, error)
GetClusterProxy() (*configv1.Proxy, error)
GetAgentServiceConfig() (*aiv1beta1.AgentServiceConfig, error)
CreateAgentClusterInstall(agentClusterInstall *hiveext.AgentClusterInstall) error
CreateNamespace(name string) error
Expand Down Expand Up @@ -203,3 +204,16 @@ func (o *LocalClusterImportOperations) GetClusterDNS() (*configv1.DNS, error) {
}
return dns, nil
}

func (o *LocalClusterImportOperations) GetClusterProxy() (*configv1.Proxy, error) {
proxy := &configv1.Proxy{}
namespacedName := types.NamespacedName{
Namespace: "",
Name: "cluster",
}
err := o.apiReader.Get(o.context, namespacedName, proxy)
if err != nil {
return nil, err
}
return proxy, nil
}
15 changes: 15 additions & 0 deletions pkg/localclusterimport/local_cluster_import_operations_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4babfb8

Please sign in to comment.