|
- /*
- Copyright 2021 The KubeEdge Authors.
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- package dataset
-
- import (
- "context"
- "encoding/json"
-
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-
- sednav1 "github.com/kubeedge/sedna/pkg/apis/sedna/v1alpha1"
- "github.com/kubeedge/sedna/pkg/globalmanager/runtime"
- )
-
- // updateFromEdge syncs update from edge
- func (c *Controller) updateFromEdge(name, namespace, operation string, content []byte) error {
- status := sednav1.DatasetStatus{}
- err := json.Unmarshal(content, &status)
- if err != nil {
- return err
- }
-
- return c.updateStatus(name, namespace, status)
- }
-
- // updateStatus updates the dataset status
- func (c *Controller) updateStatus(name, namespace string, status sednav1.DatasetStatus) error {
- client := c.client.Datasets(namespace)
-
- if status.UpdateTime == nil {
- now := metav1.Now()
- status.UpdateTime = &now
- }
-
- return runtime.RetryUpdateStatus(name, namespace, func() error {
- dataset, err := client.Get(context.TODO(), name, metav1.GetOptions{})
- if err != nil {
- return err
- }
- dataset.Status = status
- _, err = client.UpdateStatus(context.TODO(), dataset, metav1.UpdateOptions{})
- return err
- })
- }
-
- func (c *Controller) addUpstreamHandler(cc *runtime.ControllerContext) error {
- return cc.UpstreamController.Add(KindName, c.updateFromEdge)
- }
|