Skip to content

Commit eba32bf

Browse files
authored
Merge pull request #367 from anchore/fix-registration
fix: integration registration should reuse the integration registration name
2 parents f2c3d54 + 62ecbb6 commit eba32bf

File tree

2 files changed

+103
-26
lines changed

2 files changed

+103
-26
lines changed

pkg/integration/integration.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ func PerformRegistration(appConfig *config.Application, ch Channels) (*Integrati
106106
name := os.Getenv("HOSTNAME")
107107

108108
k8sClient := getK8sClient(appConfig)
109-
registrationInfo := getRegistrationInfo(appConfig, k8sClient, namespace, name, uuid.New, time.Now)
109+
replicaCount, err := getReplicaCountFromK8s(k8sClient, namespace, name)
110+
if err != nil {
111+
log.Errorf("Failed to get replica count from K8s: %v", err)
112+
}
113+
log.Debugf("Determined replica count from K8s: %d", replicaCount)
114+
registrationInfo := getRegistrationInfo(appConfig, k8sClient, namespace, name, replicaCount, uuid.New, time.Now)
110115

111116
// Register this agent with enterprise
112117
registeredIntegration, err := register(registrationInfo, appConfig.AnchoreDetails, -1,
@@ -292,7 +297,7 @@ func doRegister(registrationInfo *Registration, anchoreDetails config.AnchoreInf
292297
}
293298

294299
func getRegistrationInfo(appConfig *config.Application, k8sClient *client.Client,
295-
namespace string, name string, newUUID _NewUUID, now _Now) *Registration {
300+
namespace string, name string, replicaCount int32, newUUID _NewUUID, now _Now) *Registration {
296301
var registrationID, registrationInstanceID, instanceName, appVersion, description string
297302

298303
log.Debugf("Attempting to determine values from K8s Deployment for Pod: %s in Namespace: %s",
@@ -309,10 +314,14 @@ func getRegistrationInfo(appConfig *config.Application, k8sClient *client.Client
309314
registrationID = newUUID().String()
310315
}
311316

312-
if name != "" {
313-
log.Debugf("Using registration_instance_id: %s", name)
317+
switch {
318+
case replicaCount != 1:
319+
log.Debugf("Could not find single replica, using registration_instance_id: %s", instanceName)
314320
registrationInstanceID = name
315-
} else {
321+
case instanceName != "":
322+
log.Debugf("Using registration_instance_id: %s", instanceName)
323+
registrationInstanceID = instanceName
324+
default:
316325
log.Debugf("Generating UUIDv4 to use as registration_instance_id")
317326
registrationInstanceID = newUUID().String()
318327
}
@@ -385,6 +394,26 @@ func getInstanceDataFromK8s(k8sClient *client.Client, namespace string, podName
385394
return registrationID, instanceName, appVersion
386395
}
387396

397+
func getReplicaCountFromK8s(k8sClient *client.Client, namespace string, podName string) (int32, error) {
398+
if k8sClient == nil {
399+
log.Errorf("Kubernetes client not initialized. Unable to interact with K8s cluster.")
400+
return 0, fmt.Errorf("kubernetes client not initialized")
401+
}
402+
opts := metav1.GetOptions{}
403+
pod, err := k8sClient.Clientset.CoreV1().Pods(namespace).Get(context.Background(), podName, opts)
404+
if err != nil {
405+
log.Errorf("failed to get pod: %v", err)
406+
return 0, err
407+
}
408+
replicaSetName := pod.ObjectMeta.OwnerReferences[0].Name
409+
replicaSet, err := k8sClient.Clientset.AppsV1().ReplicaSets(namespace).Get(context.Background(), replicaSetName, opts)
410+
if err != nil {
411+
log.Errorf("failed to get replica set: %v", err)
412+
return 0, err
413+
}
414+
return *replicaSet.Spec.Replicas, nil
415+
}
416+
388417
func getAccountsAndNamespacesForAgent(appConfig *config.Application) ([]string, []string) {
389418
accountSet := make(map[string]bool)
390419
namespaceSet := make(map[string]bool)

pkg/integration/integration_test.go

Lines changed: 69 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package integration
22

33
import (
44
"fmt"
5+
"net"
6+
"net/http"
7+
"net/url"
8+
"os"
9+
"slices"
10+
"syscall"
11+
"testing"
12+
"time"
13+
514
"github.com/anchore/k8s-inventory/internal/anchore"
615
"github.com/anchore/k8s-inventory/internal/config"
716
jstime "github.com/anchore/k8s-inventory/internal/time"
@@ -13,14 +22,6 @@ import (
1322
v1 "k8s.io/api/core/v1"
1423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1524
"k8s.io/client-go/kubernetes/fake"
16-
"net"
17-
"net/http"
18-
"net/url"
19-
"os"
20-
"slices"
21-
"syscall"
22-
"testing"
23-
"time"
2425
)
2526

2627
var (
@@ -605,10 +606,11 @@ func TestGetRegistrationInfo(t *testing.T) {
605606
timestamps := []time.Time{time.Now()}
606607

607608
type args struct {
608-
config *config.Application
609-
c *client.Client
610-
namespace string
611-
name string
609+
config *config.Application
610+
c *client.Client
611+
namespace string
612+
name string
613+
replicaCount int32
612614
}
613615
tests := []struct {
614616
name string
@@ -632,9 +634,10 @@ func TestGetRegistrationInfo(t *testing.T) {
632634
},
633635
HealthReportIntervalSeconds: 60,
634636
},
635-
c: nil,
636-
namespace: "test-namespace",
637-
name: "",
637+
c: nil,
638+
namespace: "test-namespace",
639+
name: "",
640+
replicaCount: 1,
638641
},
639642
want: &Registration{
640643
RegistrationID: uuids[0].String(),
@@ -676,9 +679,10 @@ func TestGetRegistrationInfo(t *testing.T) {
676679
},
677680
HealthReportIntervalSeconds: 60,
678681
},
679-
c: nil,
680-
namespace: "test-namespace",
681-
name: "1111223344",
682+
c: nil,
683+
namespace: "test-namespace",
684+
name: "1111223344",
685+
replicaCount: 0,
682686
},
683687
want: &Registration{
684688
RegistrationID: "test-registration-id",
@@ -719,8 +723,9 @@ func TestGetRegistrationInfo(t *testing.T) {
719723
c: &client.Client{
720724
Clientset: fake.NewSimpleClientset(&pod, &replicaSet, &deployment),
721725
},
722-
namespace: "test-namespace",
723-
name: "test-pod",
726+
namespace: "test-namespace",
727+
name: "test-pod",
728+
replicaCount: 2,
724729
},
725730
want: &Registration{
726731
RegistrationID: "test-deployment-uid",
@@ -740,6 +745,49 @@ func TestGetRegistrationInfo(t *testing.T) {
740745
HealthReportInterval: 60,
741746
},
742747
},
748+
{
749+
name: "Values from k8s single replica",
750+
args: args{
751+
config: &config.Application{
752+
AnchoreDetails: config.AnchoreInfo{
753+
User: "admin",
754+
},
755+
AccountRoutes: config.AccountRoutes{
756+
"account3": config.AccountRouteDetails{
757+
Namespaces: []string{"ns3"},
758+
},
759+
},
760+
KubeConfig: config.KubeConf{
761+
Cluster: "k8s-cluster1",
762+
},
763+
Registration: config.RegistrationOptions{},
764+
HealthReportIntervalSeconds: 60,
765+
},
766+
c: &client.Client{
767+
Clientset: fake.NewSimpleClientset(&pod, &replicaSet, &deployment),
768+
},
769+
namespace: "test-namespace",
770+
name: "test-pod",
771+
replicaCount: 1,
772+
},
773+
want: &Registration{
774+
RegistrationID: "test-deployment-uid",
775+
RegistrationInstanceID: deployment.ObjectMeta.Name,
776+
Type: Type,
777+
Name: "test-deployment-k8s-inventory",
778+
Description: "",
779+
Version: "1.7.0",
780+
StartedAt: jstime.Datetime{Time: timestamps[0].UTC()},
781+
Uptime: new(jstime.Duration),
782+
Username: "admin",
783+
ExplicitlyAccountBound: []string{"account3"},
784+
Namespaces: []string{"ns3"},
785+
Configuration: nil,
786+
ClusterName: "k8s-cluster1",
787+
Namespace: "test-namespace",
788+
HealthReportInterval: 60,
789+
},
790+
},
743791
}
744792
for _, tt := range tests {
745793
t.Run(tt.name, func(t *testing.T) {
@@ -756,7 +804,7 @@ func TestGetRegistrationInfo(t *testing.T) {
756804
return timestamp
757805
}
758806
result := getRegistrationInfo(tt.args.config, tt.args.c, tt.args.namespace,
759-
tt.args.name, NewUUIDMock, nowMock)
807+
tt.args.name, tt.args.replicaCount, NewUUIDMock, nowMock)
760808
assert.NotNil(t, result)
761809
assert.Equal(t, tt.want, result)
762810
})

0 commit comments

Comments
 (0)