Skip to content

Commit

Permalink
feat: add comment for public method and function
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <[email protected]>
  • Loading branch information
hlts2 committed Dec 24, 2024
1 parent 9c521a4 commit 92dc3a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions pkg/driver/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// Hook defines the lifecycle methods for a hook, such as PreStop, PostStart, and PostStop.
// Implementations of this interface can define actions to be performed at different lifecycle stages.
type Hook interface {
PreStop(ctx context.Context) error
// PostStop, ...
}

type hook struct {
Expand All @@ -21,12 +22,12 @@ type hook struct {
clientCfgPath string
}

// NewHook creates a new Hook with the provided options. It returns an error if setup fails.
func NewHook(opts ...Option) (Hook, error) {
h := &hook{}
for _, opt := range append(defaultOpts, opts...) {
opt(h)
}

if h.nodeName == "" {
return nil, errors.New("node name not found")
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/driver/hooks/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"k8s.io/client-go/kubernetes"
)

// Option represents a configuration function that modifies hook object.
type Option func(*hook)

var defaultOpts = []Option{
Expand All @@ -21,6 +22,7 @@ func WithKubernetesClient(client kubernetes.Interface) Option {
}
}

// WithKubernetesClient returns Option to set Kubernetes config path.
func WithKubernetesClientConfigPath(path string) Option {
return func(h *hook) {
if path != "" {
Expand All @@ -29,7 +31,7 @@ func WithKubernetesClientConfigPath(path string) Option {
}
}

// WithNodeName returns Option to set node node name.
// WithNodeName returns Option to set node name.
func WithNodeName(name string) Option {
return func(h *hook) {
if name != "" {
Expand Down
6 changes: 5 additions & 1 deletion pkg/driver/hooks/prestop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ var drainTaints = map[string]struct{}{
v1.TaintNodeUnschedulable: {}, // Kubernetes common eviction taint (kubectl drain)
}

// PreStop handles the PreStop lifecycle event. It retrieves the node information
// from the Kubernetes API and checks if the node is being drained. If the node is not
// being drained, it skips the VolumeAttachment cleanup check. If the node is being drained,
// it waits for the cleanup of VolumeAttachments. If any errors occur during this process,
// they are logged and returned.
func (h *hook) PreStop(ctx context.Context) error {
node, err := h.client.CoreV1().Nodes().Get(ctx, h.nodeName, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -49,7 +54,6 @@ func (h *hook) PreStop(ctx context.Context) error {
log.Info().
Str("node_name", h.nodeName).
Msg("Finished waiting for VolumeAttachments cleanup")

return nil
}

Expand Down

0 comments on commit 92dc3a7

Please sign in to comment.