Skip to content
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

Include capabilities in CSIAddonsNode status #635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/csiaddons/v1alpha1/csiaddonsnode_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ type CSIAddonsNodeStatus struct {
// for machine parsing and tidy display in the CLI.
// +optional
Reason string `json:"reason,omitempty"`

// A list of capabilities advertised by the sidecar
Capabilities []string `json:"capabilities,omitempty"`
}

//+kubebuilder:object:root=true
Expand Down
7 changes: 6 additions & 1 deletion api/csiaddons/v1alpha1/zz_generated.deepcopy.go

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

5 changes: 5 additions & 0 deletions config/crd/bases/csiaddons.openshift.io_csiaddonsnodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ spec:
status:
description: CSIAddonsNodeStatus defines the observed state of CSIAddonsNode
properties:
capabilities:
description: A list of capabilities advertised by the sidecar
items:
type: string
type: array
message:
description: |-
Message is a human-readable message indicating details about why the CSIAddonsNode
Expand Down
5 changes: 5 additions & 0 deletions deploy/controller/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ spec:
status:
description: CSIAddonsNodeStatus defines the observed state of CSIAddonsNode
properties:
capabilities:
description: A list of capabilities advertised by the sidecar
items:
type: string
type: array
message:
description: |-
Message is a human-readable message indicating details about why the CSIAddonsNode
Expand Down
22 changes: 22 additions & 0 deletions internal/controller/csiaddons/csiaddonsnode_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1"
"github.com/csi-addons/kubernetes-csi-addons/internal/connection"
"github.com/csi-addons/kubernetes-csi-addons/internal/util"
"github.com/csi-addons/spec/lib/go/identity"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -142,6 +143,7 @@ func (r *CSIAddonsNodeReconciler) Reconcile(ctx context.Context, req ctrl.Reques

csiAddonsNode.Status.State = csiaddonsv1alpha1.CSIAddonsNodeStateConnected
csiAddonsNode.Status.Message = "Successfully established connection with sidecar"
csiAddonsNode.Status.Capabilities = parseCapabilities(newConn.Capabilities)
err = r.Client.Status().Update(ctx, csiAddonsNode)
if err != nil {
logger.Error(err, "Failed to update status")
Expand Down Expand Up @@ -271,3 +273,23 @@ func validateCSIAddonsNodeSpec(csiaddonsnode *csiaddonsv1alpha1.CSIAddonsNode) e

return nil
}

// parseCapabilities returns a list of capabilities in the format
// capability.Type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a little note/example of how a cap.String() looks like, and what the desired output for it is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// e.g. A cap.String with value "service:{type:NODE_SERVICE}"
// Will be parsed and returned as "service.NODE_SERVICE"
func parseCapabilities(caps []*identity.Capability) []string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add unit test for this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if len(caps) == 0 {
return []string{}
}

capabilities := make([]string, len(caps))

for i, cap := range caps {
capStr := strings.ReplaceAll(cap.String(), ":{type:", ".")
capStr = strings.ReplaceAll(capStr, "}", "")
capabilities[i] = capStr
}

return capabilities
}
76 changes: 76 additions & 0 deletions internal/controller/csiaddons/csiaddonsnode_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"errors"
"testing"

"github.com/csi-addons/spec/lib/go/identity"

"github.com/stretchr/testify/assert"
)

Expand All @@ -42,3 +44,77 @@ func TestParseEndpoint(t *testing.T) {
_, _, _, err = parseEndpoint("pod://pod.ns.cluster.local:5678")
assert.Error(t, err)
}

func TestParseCapabilities(t *testing.T) {
tests := []struct {
name string
caps []*identity.Capability
expected []string
}{
{
name: "Empty capabilities",
caps: []*identity.Capability{},
expected: []string{},
},
{
name: "Single capability",
caps: []*identity.Capability{
{
Type: &identity.Capability_Service_{
Service: &identity.Capability_Service{
Type: identity.Capability_Service_NODE_SERVICE,
},
},
},
},
expected: []string{"service.NODE_SERVICE"},
},
{
name: "Multiple capabilities",
caps: []*identity.Capability{
{
Type: &identity.Capability_Service_{
Service: &identity.Capability_Service{
Type: identity.Capability_Service_NODE_SERVICE,
},
},
},
{
Type: &identity.Capability_ReclaimSpace_{
ReclaimSpace: &identity.Capability_ReclaimSpace{
Type: identity.Capability_ReclaimSpace_ONLINE,
},
},
},
},
expected: []string{"service.NODE_SERVICE", "reclaim_space.ONLINE"},
},
{
name: "Same capability with different types",
caps: []*identity.Capability{
{
Type: &identity.Capability_ReclaimSpace_{
ReclaimSpace: &identity.Capability_ReclaimSpace{
Type: identity.Capability_ReclaimSpace_ONLINE,
},
},
},
{
Type: &identity.Capability_ReclaimSpace_{
ReclaimSpace: &identity.Capability_ReclaimSpace{
Type: identity.Capability_ReclaimSpace_OFFLINE,
},
},
},
},
expected: []string{"reclaim_space.ONLINE", "reclaim_space.OFFLINE"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := parseCapabilities(tt.caps)
assert.Equal(t, tt.expected, result)
})
}
}
Loading