-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.go
158 lines (158 loc) · 8.11 KB
/
docs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Package worker provides a set of tools designed to facilitate the interaction with
// Kubernetes resources from within a cluster. It offers a convenient abstraction for
// managing Kubernetes operations, focusing on pod health checks, pod labeling, structured logging,
// scaling deployments, updating deployment images, creating PersistentVolumeClaims (PVCs),
// updating network policies, and task configuration through YAML or JSON files.
//
// The package is intended for applications running as pods within Kubernetes clusters
// and leverages in-cluster configuration to establish a clientset for API interactions.
//
// Enhancements in the latest version:
//
// - Retry policies have been introduced to provide a robust mechanism for handling
// transient failures when performing Kubernetes operations. The RetryPolicy struct
// allows for configuring the maximum number of retries and the delay between attempts,
// ensuring that temporary issues can be overcome without manual intervention.
//
// - Structured logging has been integrated throughout the package, providing clear
// and consistent logging messages that are easier to read and debug. Logging now
// includes emojis for quick visual parsing and additional context such as task names
// and worker indices.
//
// - The dynamic task execution model allows for registering and retrieving TaskRunner
// implementations based on task types. This extensibility makes it possible to easily
// add new task handling logic without modifying the core package code.
//
// - Pod Labeling Logic has been optimized to check existing labels and only update
// when necessary, reducing API calls and improving performance. It also includes
// retry logic to handle intermittent API errors.
//
// - Configuration loading from YAML files has been added, enhancing the flexibility
// and configurability of task management within the worker processes.
//
// - Scaling deployments is now supported with functions that allow for adjusting the
// number of replicas with retry logic to handle conflicts.
//
// - Updating deployment images has been introduced, enabling the change of container images
// within deployments. This includes handling retries on update conflicts and reporting
// the outcome of the operation.
//
// - Creation of PersistentVolumeClaims (PVCs) is now supported, allowing for dynamic
// provisioning of storage resources within the cluster. The process is logged with
// emojis to indicate success or failure, improving the visibility of the operation's outcome.
//
// - Network Policy updates can now be handled, allowing for the modification of network
// policies within the cluster to manage traffic flow between pods/services.
//
// # Functions
//
// - NewKubernetesClient: Creates a new Kubernetes clientset configured for in-cluster
// communication with the Kubernetes API server.
//
// - CrewWorker: Orchestrates a worker process to perform tasks such as health checks,
// labeling of pods, scaling deployments, updating deployment images, creating PVCs,
// updating network policies, and other configurable tasks within a specified namespace.
// It includes retry logic to handle transient errors and respects cancellation and timeout contexts.
// Structured logging is used to provide detailed contextual information, now with emojis for better visual cues.
//
// - LoadTasksFromYAML: Loads task configurations from a YAML file, allowing for
// dynamic task management based on external configuration.
//
// - CrewGetPods: Retrieves all pods within a given namespace, logging the attempt
// and outcome of the operation, now with emojis for quick status recognition.
//
// - CrewProcessPods: Iterates over a collection of pods, assessing their health,
// updating labels, and reporting the status to a results channel. It also handles context cancellation.
//
// - CrewCheckingisPodHealthy: Evaluates the health of a pod based on its phase and
// container readiness statuses.
//
// - CrewLabelPods: Updates the labels on a pod, if necessary, based on the provided
// labeling rules and specifications.
//
// - CrewScaleDeployment: Scales a Kubernetes deployment to a specified number of replicas,
// with retries on conflicts. It provides detailed logs and returns success or failure
// messages through a results channel.
//
// - UpdateDeploymentImage: Updates the image of a specified container within a deployment,
// handling retries on conflicts and reporting the outcome through a results channel.
//
// - CrewCreatePVCStorage: Creates a PersistentVolumeClaim in the specified namespace,
// allowing for storage provisioning according to the parameters provided.
// The creation process is logged with success and error emojis for immediate feedback.
//
// - CrewUpdateNetworkPolicy: Updates a Kubernetes NetworkPolicy based on the provided
// parameters, handling retries on conflicts and reporting the outcome through a results channel.
// It logs the update process with structured logging, including emojis for visual cues.
//
// Usage:
//
// Initialize the Kubernetes client using NewKubernetesClient, then leverage the client
// to perform operations such as retrieving and processing pods within a namespace, scaling
// deployments, updating deployment images, creating PVCs, updating network policies, and
// more as required. Contexts are used to manage the lifecycle of the worker processes,
// including graceful shutdowns and cancellation. Task configurations can be loaded from
// a YAML file for enhanced flexibility.
//
// Example:
//
// clientset, err := worker.NewKubernetesClient()
// if err != nil {
// // Handle error
// }
// namespace := "default" // Replace with your namespace
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel() // Ensure cancellation is called to free resources
//
// tasks, err := worker.LoadTasksFromYAML("tasks.yaml")
// if err != nil {
// // Handle error
// }
//
// resultsChan := make(chan string)
// go worker.CrewWorker(ctx, clientset, namespace, tasks, resultsChan)
//
// // Process results as they come in
// for result := range resultsChan {
// fmt.Println(result)
// }
//
// # Enhancements
//
// - The package now includes structured logging capabilities, enhanced with emojis, improving traceability
// and aiding in debugging efforts by providing detailed contextual information.
//
// - Logging functionality is customizable, allowing different workers to provide
// unique contextual information, such as worker indices or specific namespaces, with visual cues.
//
// - The dynamic task execution model supports adding new tasks and task runners
// without changing existing code, facilitating scalability and extensibility.
//
// - Pod Labeling Logic has been enhanced to perform more efficiently by minimizing
// unnecessary API calls, and it now includes robust error handling and retry mechanisms.
//
// - The scaling functionality has been introduced to adjust deployment sizes with
// conflict resolution strategies, catering to dynamic workload requirements.
//
// - Image update functionality has been added to modify the image of a container within
// a deployment, with built-in retry logic for handling update conflicts.
//
// - The introduction of PVC creation allows for dynamic storage provisioning, complete with
// emoji-based logging for immediate operation feedback.
//
// - Network Policy update functionality has been introduced, allowing for the management
// of network traffic policies within the cluster, with structured logging and retry mechanisms.
//
// # TODO
//
// - Extend the functionality of the CrewWorker function to support a wider range
// of tasks and allow for greater configurability.
//
// - Expand the package's support for additional Kubernetes resources and operations,
// catering to more complex orchestration needs.
//
// - Introduce metrics collection to monitor the health and performance of worker
// processes, facilitating proactive maintenance and scaling decisions.
//
// Copyright (c) 2023 H0llyW00dzZ
package worker