Skip to content

Commit

Permalink
feat: add timestamp for virtualcluster
Browse files Browse the repository at this point in the history
Signed-off-by: OrangeBao <[email protected]>
  • Loading branch information
OrangeBao committed Apr 23, 2024
1 parent 5a94d1b commit 138c13a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 22 deletions.
12 changes: 11 additions & 1 deletion deploy/crds/kosmos.io_virtualclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ spec:
singular: virtualcluster
scope: Namespaced
versions:
- name: v1alpha1
- additionalPrinterColumns:
- jsonPath: .status.phase
name: STATUS
type: string
name: v1alpha1
schema:
openAPIV3Schema:
properties:
Expand Down Expand Up @@ -133,9 +137,15 @@ spec:
phase:
description: Phase is the phase of kosmos-operator handling the VirtualCluster
type: string
reason:
type: string
timeStamp:
format: date-time
type: string
type: object
required:
- spec
type: object
served: true
storage: true
subresources: {}
3 changes: 3 additions & 0 deletions pkg/apis/kosmos/v1alpha1/virtualcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
// +genclient
// +kubebuilder:resource:scope=Namespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:printcolumn:name="STATUS",type=string,JSONPath=`.status.phase`

type VirtualCluster struct {
metav1.TypeMeta `json:",inline"`
Expand Down Expand Up @@ -89,6 +90,8 @@ type VirtualClusterStatus struct {
Phase Phase `json:"phase,omitempty"`
// +optional
Reason string `json:"reason,omitempty" protobuf:"bytes,4,opt,name=reason"`
// +optional
TimeStamp *metav1.Time `json:"timeStamp,omitempty" protobuf:"bytes,7,opt,name=timeStamp"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/kosmos/v1alpha1/zz_generated.deepcopy.go

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

13 changes: 13 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *NodeController) joinNode(ctx context.Context, nodeInfos []vcrnodepoolco
// step(2/5) scp ca of virtualcluster
nn := types.NamespacedName{
Namespace: virtualCluster.Namespace,
Name: fmt.Sprintf("%s-cert", virtualCluster.Namespace),
Name: fmt.Sprintf("%s-cert", virtualCluster.Name),
}
targetCert := &v1.Secret{}
if err := r.Get(ctx, nn, targetCert); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func compareAndTranformNodes(targetNodes []v1alpha1.NodeInfo, actualNodes []v1.N
if !ok {
return nil, nil, fmt.Errorf("node %s not found in node pool", actualNode.Name)
}

unjoinNodes = append(unjoinNodes, nodePool)
}
}
Expand All @@ -140,7 +141,7 @@ func (r *NodeController) GetNodePool(ctx context.Context) (map[string]vcrnodepoo
return nil, fmt.Errorf("get node-pool failed: %v", err)
}

nodePools, err := vcrnodepoolcontroller.ConvertJsonToNodeItem(nodePool.Data[NodePoolCMKeyName])
nodePools, err := vcrnodepoolcontroller.ConvertYamlToNodeItem(nodePool.Data[NodePoolCMKeyName])
if err != nil {
return nil, fmt.Errorf("convert node-pool failed: %v", err)
}
Expand All @@ -152,6 +153,8 @@ func (r *NodeController) UpdateVirtualClusterStatus(ctx context.Context, virtual
updateVirtualCluster := virtualCluster.DeepCopy()
updateVirtualCluster.Status.Phase = status
updateVirtualCluster.Status.Reason = reason
timestamp := metav1.Now()
updateVirtualCluster.Status.TimeStamp = &timestamp

if err := r.Update(ctx, updateVirtualCluster); err != nil {
return fmt.Errorf("update virtualcluster %s status failed: %s", virtualCluster.Name, err)
Expand Down Expand Up @@ -182,8 +185,14 @@ func (r *NodeController) DoNodeTask(ctx context.Context, virtualCluster v1alpha1
}

if len(unjoinNodes) > 0 || len(joinNodes) > 0 {
if err := r.UpdateVirtualClusterStatus(ctx, virtualCluster, v1alpha1.Updating, "node task"); err != nil {
return err
if virtualCluster.Status.Phase == v1alpha1.Initialized {
if err := r.UpdateVirtualClusterStatus(ctx, virtualCluster, v1alpha1.Initialized, "node init"); err != nil {
return err
}
} else {
if err := r.UpdateVirtualClusterStatus(ctx, virtualCluster, v1alpha1.Updating, "node task"); err != nil {
return err
}
}
}
if len(unjoinNodes) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (r *NodeController) UpdateNodePoolState(ctx context.Context, nodeName strin

updateNodePool := nodePool.DeepCopy()

jsonStr := updateNodePool.Data[NodePoolCMKeyName]
nodePoolItem, err := vcrnodepoolcontroller.ConvertJsonToNodePoolItem(jsonStr)
yamlStr := updateNodePool.Data[NodePoolCMKeyName]
nodePoolItem, err := vcrnodepoolcontroller.ConvertYamlToNodePoolItem(yamlStr)
if err != nil {
return err
}
Expand All @@ -32,7 +32,7 @@ func (r *NodeController) UpdateNodePoolState(ctx context.Context, nodeName strin

nodePoolItem[nodeName] = targetNodePoolItem

nodePoolBytes, err := vcrnodepoolcontroller.ConvertNodePoolItemToJson(nodePoolItem)
nodePoolBytes, err := vcrnodepoolcontroller.ConvertNodePoolItemToYaml(nodePoolItem)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package vcrnodepoolcontroller

import (
"encoding/json"
"gopkg.in/yaml.v3"
)

type NodePoolMapItem struct {
Address string `json:"address"`
Labels map[string]string `json:"labels"`
Cluster string `json:"cluster"`
State string `json:"state"`
Address string `yaml:"address"`
Labels map[string]string `yaml:"labels"`
Cluster string `yaml:"cluster"`
State string `yaml:"state"`
}

type NodeItem struct {
NodePoolMapItem
Name string `json:"-"`
Name string `yaml:"-"`
}

func ConvertJsonToNodeItem(jsonStr string) (map[string]NodeItem, error) {
func ConvertYamlToNodeItem(yamlStr string) (map[string]NodeItem, error) {
nodepoolMap := map[string]NodeItem{}

nodepoolItem, err := ConvertJsonToNodePoolItem(jsonStr)
nodepoolItem, err := ConvertYamlToNodePoolItem(yamlStr)
if err != nil {
return nil, err
}
Expand All @@ -34,21 +34,21 @@ func ConvertJsonToNodeItem(jsonStr string) (map[string]NodeItem, error) {
return nodepoolMap, nil
}

func ConvertJsonToNodePoolItem(jsonStr string) (map[string]NodePoolMapItem, error) {
func ConvertYamlToNodePoolItem(yamlStr string) (map[string]NodePoolMapItem, error) {
nodepoolItem := map[string]NodePoolMapItem{}
err := json.Unmarshal([]byte(jsonStr), &nodepoolItem)
err := yaml.Unmarshal([]byte(yamlStr), &nodepoolItem)
if err != nil {
return nil, err
}
return nodepoolItem, nil
}

func ConvertNodePoolItemToJson(nodepoolItem map[string]NodePoolMapItem) ([]byte, error) {
jsonStr, err := json.Marshal(nodepoolItem)
func ConvertNodePoolItemToYaml(nodepoolItem map[string]NodePoolMapItem) ([]byte, error) {
yamlStr, err := yaml.Marshal(nodepoolItem)
if err != nil {
return nil, err
}
return jsonStr, nil
return yamlStr, nil
}

// controller task
Expand Down

0 comments on commit 138c13a

Please sign in to comment.