Skip to content

Commit

Permalink
Gather architectures from k8s API
Browse files Browse the repository at this point in the history
  • Loading branch information
joselsegura committed Aug 13, 2024
1 parent 8024649 commit 6c3de61
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
return fmt.Errorf("unable to set initial cluster status: %v", err)
}

scaController := sca.New(ctx, kubeClient.CoreV1(), configAggregator, insightsClient)
scaController := sca.New(ctx, kubeClient.CoreV1(), configAggregator, insightsClient, gatherProtoKubeConfig)
statusReporter.AddSources(scaController)
go scaController.Run()

Expand Down
29 changes: 29 additions & 0 deletions pkg/ocm/sca/architectures_gather.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sca

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

func gatherArchitectures(ctx context.Context, protoConfig *rest.Config) (map[string]struct{}, error) {
gatherKubeClient, err := kubernetes.NewForConfig(protoConfig)
if err != nil {
return nil, err
}

coreClient := gatherKubeClient.CoreV1()
nodes, err := coreClient.Nodes().List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}

architectures := make(map[string]struct{})
for i := range nodes.Items {
nodeArch := nodes.Items[i].Status.NodeInfo.Architecture
architectures[nodeArch] = struct{}{}
}
return architectures, nil
}
28 changes: 18 additions & 10 deletions pkg/ocm/sca/sca.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"

"github.com/openshift/insights-operator/pkg/config/configobserver"
Expand All @@ -33,10 +34,11 @@ const (
// Controller holds all the required resources to be able to communicate with OCM API
type Controller struct {
controllerstatus.StatusController
coreClient corev1client.CoreV1Interface
ctx context.Context
configurator configobserver.Interface
client *insightsclient.Client
coreClient corev1client.CoreV1Interface
ctx context.Context
configurator configobserver.Interface
client *insightsclient.Client
gatherProtoKubeConfig *rest.Config
}

// Response structure is used to unmarshall the OCM SCA response. It holds the SCA certificate
Expand All @@ -49,13 +51,14 @@ type Response struct {

// New creates new instance
func New(ctx context.Context, coreClient corev1client.CoreV1Interface, configurator configobserver.Interface,
insightsClient *insightsclient.Client) *Controller {
insightsClient *insightsclient.Client, gatherProtoKubeConfig *rest.Config) *Controller {
return &Controller{
StatusController: controllerstatus.New(ControllerName),
coreClient: coreClient,
ctx: ctx,
configurator: configurator,
client: insightsClient,
StatusController: controllerstatus.New(ControllerName),
coreClient: coreClient,
ctx: ctx,
configurator: configurator,
client: insightsClient,
gatherProtoKubeConfig: gatherProtoKubeConfig,
}
}

Expand Down Expand Up @@ -219,6 +222,11 @@ func (c *Controller) requestSCAWithExpBackoff(endpoint string) ([]byte, error) {
var data []byte
err := wait.ExponentialBackoff(bo, func() (bool, error) {
var err error
ctx := context.TODO()
_, err = gatherArchitectures(ctx, c.gatherProtoKubeConfig)
if err != nil {
return true, err
}
data, err = c.client.RecvSCACerts(c.ctx, endpoint)
if err != nil {
// don't try again in case it's not an HTTP error - it could mean we're in disconnected env
Expand Down
4 changes: 2 additions & 2 deletions pkg/ocm/sca/sca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
func Test_SCAController_SecretIsCreated(t *testing.T) {
kube := kubefake.NewSimpleClientset()
coreClient := kube.CoreV1()
scaController := New(context.TODO(), coreClient, nil, nil)
scaController := New(context.TODO(), coreClient, nil, nil, nil)

testRes := &Response{
Key: "secret key",
Expand Down Expand Up @@ -52,7 +52,7 @@ func Test_SCAController_SecretIsUpdated(t *testing.T) {
}
_, err := coreClient.Secrets(targetNamespaceName).Create(context.Background(), existingSec, metav1.CreateOptions{})
assert.NoError(t, err)
scaController := New(context.TODO(), coreClient, nil, nil)
scaController := New(context.TODO(), coreClient, nil, nil, nil)
testRes := &Response{
Key: "new secret testing key",
Cert: "new secret testing cert",
Expand Down

0 comments on commit 6c3de61

Please sign in to comment.