-
Notifications
You must be signed in to change notification settings - Fork 156
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
[k8s plugin] Prepare for implementing livestate apis #5510
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c40172
Add GetLiveResources function to retrieve live resources for an appli…
Warashi 199a969
Refactor K8s sync stage to use GetLiveResources for improved resource…
Warashi 422863c
Add BuildApplicationLiveState and helper functions for application li…
Warashi 03894d4
Add comments
Warashi ffb8dbb
Add tests for BuildApplicationLiveState
Warashi 1ab511c
Fix imports
Warashi 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 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
91 changes: 91 additions & 0 deletions
91
pkg/app/pipedv1/plugin/kubernetes/provider/liveresources.go
This file contains 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,91 @@ | ||
// Copyright 2024 The PipeCD Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/model" | ||
) | ||
|
||
// GetLiveResources returns all live resources that belong to the given application. | ||
func GetLiveResources(ctx context.Context, kubectl *Kubectl, kubeconfig string, appID string, selector ...string) (namespaceScoped []Manifest, clusterScoped []Manifest, _ error) { | ||
namespacedLiveResources, err := kubectl.GetAll(ctx, kubeconfig, | ||
"", | ||
fmt.Sprintf("%s=%s", LabelManagedBy, ManagedByPiped), | ||
fmt.Sprintf("%s=%s", LabelApplication, appID), | ||
) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed while listing all namespace-scoped resources (%v)", err) | ||
} | ||
|
||
clusterScopedLiveResources, err := kubectl.GetAllClusterScoped(ctx, kubeconfig, | ||
fmt.Sprintf("%s=%s", LabelManagedBy, ManagedByPiped), | ||
fmt.Sprintf("%s=%s", LabelApplication, appID), | ||
) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed while listing all cluster-scoped resources (%v)", err) | ||
} | ||
|
||
return namespacedLiveResources, clusterScopedLiveResources, nil | ||
} | ||
|
||
// BuildApplicationLiveState builds the live state of the application from the given manifests. | ||
func BuildApplicationLiveState(deploytarget string, manifests []Manifest, now time.Time) *model.ApplicationLiveState { | ||
if len(manifests) == 0 { | ||
return &model.ApplicationLiveState{ | ||
HealthStatus: model.ApplicationLiveState_UNKNOWN, | ||
} | ||
} | ||
|
||
states := make([]*model.ResourceState, 0, len(manifests)) | ||
for _, m := range manifests { | ||
states = append(states, buildResourceState(m, now)) | ||
} | ||
|
||
return &model.ApplicationLiveState{ | ||
Resources: states, | ||
HealthStatus: model.ApplicationLiveState_UNKNOWN, // TODO: Implement health status calculation | ||
} | ||
} | ||
|
||
// buildResourceState builds the resource state from the given manifest. | ||
func buildResourceState(m Manifest, now time.Time) *model.ResourceState { | ||
var parents []string // default as nil | ||
if len(m.body.GetOwnerReferences()) > 0 { | ||
parents = make([]string, 0, len(m.body.GetOwnerReferences())) | ||
for _, o := range m.body.GetOwnerReferences() { | ||
parents = append(parents, string(o.UID)) | ||
} | ||
} | ||
|
||
return &model.ResourceState{ | ||
Id: string(m.body.GetUID()), | ||
Name: m.body.GetName(), | ||
ParentIds: parents, | ||
HealthStatus: model.ResourceState_UNKNOWN, // TODO: Implement health status calculation | ||
HealthDescription: "", // TODO: Implement health status calculation | ||
ResourceType: m.body.GetKind(), | ||
ResourceMetadata: map[string]string{ | ||
"Namespace": m.body.GetNamespace(), | ||
"API Version": m.body.GetAPIVersion(), | ||
"Kind": m.body.GetKind(), | ||
}, | ||
CreatedAt: m.body.GetCreationTimestamp().Unix(), | ||
UpdatedAt: now.Unix(), | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
pkg/app/pipedv1/plugin/kubernetes/provider/liveresources_test.go
This file contains 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,195 @@ | ||
// Copyright 2024 The PipeCD Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package provider | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/model" | ||
) | ||
|
||
func TestBuildApplicationLiveState(t *testing.T) { | ||
now := time.Now() | ||
|
||
tests := []struct { | ||
name string | ||
manifests []Manifest | ||
want *model.ApplicationLiveState | ||
}{ | ||
{ | ||
name: "single pod", | ||
manifests: []Manifest{ | ||
{ | ||
body: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "v1", | ||
"kind": "Pod", | ||
"metadata": map[string]interface{}{ | ||
"name": "test-pod", | ||
"namespace": "default", | ||
"uid": "test-uid", | ||
"creationTimestamp": now.Format(time.RFC3339), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &model.ApplicationLiveState{ | ||
Resources: []*model.ResourceState{ | ||
{ | ||
Id: "test-uid", | ||
Name: "test-pod", | ||
ResourceType: "Pod", | ||
ResourceMetadata: map[string]string{ | ||
"Namespace": "default", | ||
"API Version": "v1", | ||
"Kind": "Pod", | ||
}, | ||
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}, | ||
}, | ||
HealthStatus: model.ApplicationLiveState_UNKNOWN, | ||
}, | ||
}, | ||
{ | ||
name: "single pod with owner references", | ||
manifests: []Manifest{ | ||
{ | ||
body: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "v1", | ||
"kind": "Pod", | ||
"metadata": map[string]interface{}{ | ||
"name": "test-pod", | ||
"namespace": "default", | ||
"uid": "test-uid", | ||
"creationTimestamp": now.Format(time.RFC3339), | ||
"ownerReferences": []interface{}{ | ||
map[string]interface{}{ | ||
"uid": "owner-uid", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &model.ApplicationLiveState{ | ||
Resources: []*model.ResourceState{ | ||
{ | ||
Id: "test-uid", | ||
Name: "test-pod", | ||
ResourceType: "Pod", | ||
ResourceMetadata: map[string]string{ | ||
"Namespace": "default", | ||
"API Version": "v1", | ||
"Kind": "Pod", | ||
}, | ||
ParentIds: []string{"owner-uid"}, | ||
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}, | ||
}, | ||
HealthStatus: model.ApplicationLiveState_UNKNOWN, | ||
}, | ||
}, | ||
{ | ||
name: "multiple resources with owner references", | ||
manifests: []Manifest{ | ||
{ | ||
body: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "v1", | ||
"kind": "Pod", | ||
"metadata": map[string]interface{}{ | ||
"name": "test-pod-1", | ||
"namespace": "default", | ||
"uid": "test-uid-1", | ||
"creationTimestamp": now.Format(time.RFC3339), | ||
"ownerReferences": []interface{}{ | ||
map[string]interface{}{ | ||
"uid": "owner-uid-1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
body: &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"apiVersion": "v1", | ||
"kind": "Service", | ||
"metadata": map[string]interface{}{ | ||
"name": "test-service", | ||
"namespace": "default", | ||
"uid": "test-uid-2", | ||
"creationTimestamp": now.Format(time.RFC3339), | ||
"ownerReferences": []interface{}{ | ||
map[string]interface{}{ | ||
"uid": "owner-uid-2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: &model.ApplicationLiveState{ | ||
Resources: []*model.ResourceState{ | ||
{ | ||
Id: "test-uid-1", | ||
Name: "test-pod-1", | ||
ResourceType: "Pod", | ||
ResourceMetadata: map[string]string{ | ||
"Namespace": "default", | ||
"API Version": "v1", | ||
"Kind": "Pod", | ||
}, | ||
ParentIds: []string{"owner-uid-1"}, | ||
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}, | ||
{ | ||
Id: "test-uid-2", | ||
Name: "test-service", | ||
ResourceType: "Service", | ||
ResourceMetadata: map[string]string{ | ||
"Namespace": "default", | ||
"API Version": "v1", | ||
"Kind": "Service", | ||
}, | ||
ParentIds: []string{"owner-uid-2"}, | ||
CreatedAt: now.Unix(), | ||
UpdatedAt: now.Unix(), | ||
}, | ||
}, | ||
HealthStatus: model.ApplicationLiveState_UNKNOWN, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := BuildApplicationLiveState("test-deploytarget", tt.manifests, now) | ||
assert.Equal(t, tt.want, got, "expected live state to be equal to the expected one") | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memo: If possible, it would be nice to implement it under the live state package.
I got the thought to hide the Manifest.body from the other package.
Not consider for now.