feat(crd): add update custom resource status
This commit is contained in:
33
internal/crd/convert.go
Normal file
33
internal/crd/convert.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package crd
|
||||
|
||||
import (
|
||||
"flink-kube-operator/internal/crd/v1alpha1"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func convertToUnstructured(obj v1alpha1.FlinkJob) (*unstructured.Unstructured, error) {
|
||||
// Convert the structured object to an unstructured map
|
||||
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert to unstructured: %v", err)
|
||||
}
|
||||
|
||||
// Create an Unstructured object from the map
|
||||
unstructuredObj := &unstructured.Unstructured{
|
||||
Object: unstructuredMap,
|
||||
}
|
||||
|
||||
return unstructuredObj, nil
|
||||
}
|
||||
|
||||
func convertFromUnstructured(in *unstructured.Unstructured) (*v1alpha1.FlinkJob, error) {
|
||||
job := &v1alpha1.FlinkJob{}
|
||||
err := runtime.DefaultUnstructuredConverter.FromUnstructured(in.Object, job)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return job, nil
|
||||
}
|
||||
@@ -2,11 +2,53 @@ package crd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flink-kube-operator/internal/crd/v1alpha1"
|
||||
"fmt"
|
||||
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"gitea.com/logicamp/lc"
|
||||
"go.uber.org/zap"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
func (crd Crd) AddEvent(jobUid types.UID, event string) {
|
||||
crd.client.UpdateStatus(context.Background(), nil, v1.UpdateOptions{})
|
||||
func (crd Crd) SetJobStatus(jobUid types.UID, status string) error {
|
||||
job := GetJob(jobUid)
|
||||
// Define the patch data (JSON Merge Patch format)
|
||||
patchData := map[string]interface{}{
|
||||
"status": v1alpha1.FlinkJobStatus{},
|
||||
}
|
||||
patchBytes, err := json.Marshal(patchData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshaling patch data: %w", err)
|
||||
}
|
||||
|
||||
// Patch the status subresource
|
||||
unstructuredJob, err := crd.client.
|
||||
Namespace(job.GetNamespace()).
|
||||
Patch(
|
||||
context.Background(),
|
||||
job.GetName(),
|
||||
types.MergePatchType, // Use MergePatchType for JSON Merge Patch
|
||||
patchBytes,
|
||||
metaV1.PatchOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
lc.Logger.Error(
|
||||
"[crd] [status] error patching custom resource status",
|
||||
zap.String("namespace", job.GetNamespace()),
|
||||
zap.Error(err),
|
||||
)
|
||||
return err
|
||||
}
|
||||
patched, err := convertFromUnstructured(unstructuredJob)
|
||||
if err != nil {
|
||||
lc.Logger.Error("[crd] [status] error in structure unstructured patched", zap.Error(err))
|
||||
}
|
||||
lc.Logger.Debug("[crd] [status] set status", zap.Any("statusUpdateObj", patched))
|
||||
if err != nil {
|
||||
lc.Logger.Error("[crd] [status] ", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ type Crd struct {
|
||||
client dynamic.NamespaceableResourceInterface
|
||||
}
|
||||
|
||||
func New() {
|
||||
func New() *Crd {
|
||||
// Get Kubernetes config
|
||||
config, err := clientcmd.BuildConfigFromFlags("", clientcmd.RecommendedHomeFile)
|
||||
if err != nil {
|
||||
@@ -37,4 +37,5 @@ func New() {
|
||||
|
||||
// Watch for FlinkJob creation
|
||||
go crd.watchFlinkJobs()
|
||||
return &crd
|
||||
}
|
||||
|
||||
@@ -15,11 +15,17 @@ type FlinkJobSpec struct {
|
||||
EntryClass string `json:"entryClass"`
|
||||
}
|
||||
|
||||
type FlinkJobStatus struct {
|
||||
JobStatus *string `json:"jobStatus,omitempty"`
|
||||
LifeCycleStatus *string `json:"lifeCycleStatus,omitempty"`
|
||||
}
|
||||
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
type FlinkJob struct {
|
||||
metaV1.TypeMeta `json:",inline"`
|
||||
metaV1.ObjectMeta `json:"metadata,omitempty"`
|
||||
Spec FlinkJobSpec `json:"spec"`
|
||||
Spec FlinkJobSpec `json:"spec"`
|
||||
Status FlinkJobStatus `json:"status"`
|
||||
}
|
||||
|
||||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||||
|
||||
Reference in New Issue
Block a user