-
Notifications
You must be signed in to change notification settings - Fork 2k
Add VersionCheck and Metadata to Agent labels #7737
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
18b9e37
Add version check and collect deployment metadata and add as labels t…
AlexFenlon 1855ae3
fix lint
AlexFenlon f36b2df
fix tests for agent version check, address comments
AlexFenlon d04d2c9
improve regex, fix tests
AlexFenlon 8447b72
Merge remote-tracking branch 'origin/main' into feat/add-metadata-to-…
AlexFenlon cc74dad
allow getting other deployment id
AlexFenlon 6de4a74
allow more than 1 owner
AlexFenlon 9ebbf6d
Merge branch 'main' into feat/add-metadata-to-agent
AlexFenlon d09dabc
fix owner.Kind to owner.name and uuid for installationID and based on…
AlexFenlon 1170dc6
Merge branch 'main' into feat/add-metadata-to-agent
AlexFenlon d4033b4
Merge branch 'main' into feat/add-metadata-to-agent
AlexFenlon ff1ee99
Merge branch 'main' into feat/add-metadata-to-agent
AlexFenlon c2104f9
Merge branch 'main' into feat/add-metadata-to-agent
AlexFenlon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
clusterInfo "github.com/nginx/kubernetes-ingress/internal/common_cluster_info" | ||
api_v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// Labels contains the metadata information needed for reporting to Agent | ||
type Labels struct { | ||
ProductName string `json:"product_name"` | ||
ProductVersion string `json:"product_version"` | ||
ClusterID string `json:"cluster_id"` | ||
DeploymentName string `json:"deployment_name"` | ||
DeploymentID string `json:"deployment_id"` | ||
DeploymentNamespace string `json:"deployment_namespace"` | ||
} | ||
|
||
func newMetadataInfo(deploymentNamespace, clusterID, deploymentID, productVersion, deploymentName string) *Labels { | ||
return &Labels{ | ||
ProductName: "nic", | ||
ProductVersion: productVersion, | ||
ClusterID: clusterID, | ||
DeploymentID: deploymentID, | ||
DeploymentName: deploymentName, | ||
DeploymentNamespace: deploymentNamespace, | ||
} | ||
} | ||
|
||
// Metadata contains required information for metadata reporting | ||
type Metadata struct { | ||
K8sClientReader kubernetes.Interface | ||
PodNSName types.NamespacedName | ||
Pod *api_v1.Pod | ||
NICVersion string | ||
} | ||
|
||
// NewMetadataReporter creates a new MetadataConfig | ||
func NewMetadataReporter(client kubernetes.Interface, pod *api_v1.Pod, version string) *Metadata { | ||
return &Metadata{ | ||
K8sClientReader: client, | ||
PodNSName: types.NamespacedName{Namespace: os.Getenv("POD_NAMESPACE"), Name: os.Getenv("POD_NAME")}, | ||
Pod: pod, | ||
NICVersion: version, | ||
} | ||
} | ||
|
||
// CollectAndWrite collects the metadata information and returns a Labels struct | ||
func (md *Metadata) CollectAndWrite(ctx context.Context) (*Labels, error) { | ||
deploymentNamespace := md.PodNSName.Namespace | ||
clusterID, err := clusterInfo.GetClusterID(ctx, md.K8sClientReader) | ||
if err != nil { | ||
return nil, fmt.Errorf("error collecting ClusterID: %w", err) | ||
} | ||
deploymentID, err := clusterInfo.GetInstallationID(ctx, md.K8sClientReader, md.PodNSName) | ||
if err != nil { | ||
return nil, fmt.Errorf("error collecting InstallationID: %w", err) | ||
} | ||
deploymentName, err := clusterInfo.GetDeploymentName(ctx, md.K8sClientReader, md.PodNSName) | ||
if err != nil { | ||
return nil, fmt.Errorf("error collecting DeploymentName: %w", err) | ||
} | ||
info := newMetadataInfo(deploymentNamespace, clusterID, deploymentID, md.NICVersion, deploymentName) | ||
return info, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package metadata | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
api_v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestNewMetadataInfo(t *testing.T) { | ||
info := newMetadataInfo("nginx-ingress", "e3a5e702-65a7-a55f-753d78cd7ff7", "555-1222-4414test-11223355", "5.0.0", "my-release") | ||
if info.ProductName != "nic" { | ||
t.Errorf("ProductName = %q, want %q", info.ProductName, "nic") | ||
} | ||
if info.DeploymentNamespace != "nginx-ingress" { | ||
t.Errorf("DeploymentNamespace = %q, want %q", info.DeploymentNamespace, "nginx-ingress") | ||
} | ||
if info.ClusterID != "e3a5e702-65a7-a55f-753d78cd7ff7" { | ||
t.Errorf("ClusterID = %q, want %q", info.ClusterID, "e3a5e702-65a7-a55f-753d78cd7ff7") | ||
} | ||
if info.DeploymentID != "555-1222-4414test-11223355" { | ||
t.Errorf("DeploymentID = %q, want %q", info.DeploymentID, "555-1222-4414test-11223355") | ||
} | ||
if info.ProductVersion != "5.0.0" { | ||
t.Errorf("ProductVersion = %q, want %q", info.ProductVersion, "5.0.0") | ||
} | ||
if info.DeploymentName != "my-release" { | ||
t.Errorf("DeploymentName = %q, want %q", info.DeploymentName, "my-release") | ||
} | ||
} | ||
|
||
func TestCollectAndWrite(t *testing.T) { | ||
pod := &api_v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
Namespace: "test-namespace", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "apps/v1", | ||
Kind: "DaemonSet", | ||
Name: "test-pod", | ||
UID: types.UID("install-123"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
if err := os.Setenv("POD_NAMESPACE", pod.Namespace); err != nil { | ||
t.Errorf("unable to set POD_NAMESPACE: %v", err) | ||
} | ||
if err := os.Setenv("POD_NAME", pod.Name); err != nil { | ||
t.Errorf("unable to set POD_NAME: %v", err) | ||
} | ||
|
||
client := fake.NewSimpleClientset( | ||
&api_v1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "kube-system", | ||
UID: types.UID("123-abc-456-def"), | ||
}, | ||
}, | ||
pod, | ||
) | ||
|
||
reporter := NewMetadataReporter(client, pod, "5.0.0") | ||
if reporter == nil { | ||
t.Fatal("expected reporter to be non-nil") | ||
} | ||
|
||
info, err := reporter.CollectAndWrite(context.TODO()) | ||
if err != nil { | ||
t.Fatalf("CollectAndWrite() error = %v", err) | ||
} | ||
if got, want := info.ProductName, "nic"; got != want { | ||
t.Errorf("ProductName = %q, want %q", got, want) | ||
} | ||
} | ||
|
||
func TestNewMetadataReporter(t *testing.T) { | ||
reporter := NewMetadataReporter( | ||
fake.NewSimpleClientset(), | ||
&api_v1.Pod{}, | ||
"5.0.0", | ||
) | ||
if reporter == nil { | ||
t.Fatal("Expected NewMetadataReporter to return non-nil") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.