-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [Orch] Add CRD RC handler * Remove print statement * Refactor to separate file * Move last function * Use CRD specific status * Add cluster agent config to RC * Correctly set product * Fix product and add logging * Add fixes for crd nil pointers * Revert accidental commit * Update dependencies and add tag getter function * Go.mod change * Reset go.mod * Update remoteconfig/state * Fix updater package and work sum * Clean up logs and force restart of DCA on CR changes * Add a lock around get and update of DDA * Improve comments and change test to use orchexp for annotation * Change to not do annotations every single time * Fill in orchestrator explorer for tests * Go mod update * Modify retry logic so it doesn't it for the entire update * Fix go.mod * Check in config crd stuff * Remove hard coded product and update go.mod * Revert go.mod back and fix errors * Go.sum update * overwrite cr by incoming data instead of appending to the old data (#1473) * feedback * feedback * rename to OrchestratorK8sCRDRemoteConfig --------- Co-authored-by: Kangyi LI <kangyi.li@datadoghq.com> Co-authored-by: levan-m <116471169+levan-m@users.noreply.github.com>
- Loading branch information
1 parent
342d9af
commit ae5b9e1
Showing
7 changed files
with
279 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package remoteconfig | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
apiequality "k8s.io/apimachinery/pkg/api/equality" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
||
"github.com/DataDog/datadog-agent/pkg/remoteconfig/state" | ||
"github.com/DataDog/datadog-operator/api/datadoghq/v2alpha1" | ||
) | ||
|
||
// CustomResourceDefinitionURLs defines model for CustomResourceDefinitionURLs. | ||
type CustomResourceDefinitionURLs struct { | ||
Crds *[]string `json:"crds,omitempty"` | ||
} | ||
|
||
func (r *RemoteConfigUpdater) crdConfigUpdateCallback(updates map[string]state.RawConfig, applyStatus func(string, state.ApplyStatus)) { | ||
ctx := context.Background() | ||
|
||
var configIDs []string | ||
for id := range updates { | ||
applyStatus(id, state.ApplyStatus{State: state.ApplyStateUnacknowledged, Error: ""}) | ||
configIDs = append(configIDs, id) | ||
} | ||
|
||
mergedUpdate, err := r.parseCRDReceivedUpdates(updates, applyStatus) | ||
if err != nil { | ||
r.logger.Error(err, "Failed to merge updates") | ||
return | ||
} | ||
|
||
if err := r.getAndUpdateDatadogAgentWithRetry(ctx, mergedUpdate, r.crdUpdateInstanceStatus); err != nil { | ||
r.logger.Error(err, "Failed to update status") | ||
applyStatus(configIDs[len(configIDs)-1], state.ApplyStatus{State: state.ApplyStateError, Error: err.Error()}) | ||
return | ||
} | ||
|
||
// Acknowledge that configs were received | ||
for _, id := range configIDs { | ||
applyStatus(id, state.ApplyStatus{State: state.ApplyStateAcknowledged, Error: ""}) | ||
} | ||
|
||
r.logger.Info("Successfully applied configuration") | ||
|
||
} | ||
|
||
func (r *RemoteConfigUpdater) parseCRDReceivedUpdates(updates map[string]state.RawConfig, applyStatus func(string, state.ApplyStatus)) (OrchestratorK8sCRDRemoteConfig, error) { | ||
// Unmarshal configs and config order | ||
crds := []string{} | ||
for _, c := range updates { | ||
if c.Metadata.Product == state.ProductOrchestratorK8sCRDs { | ||
rcCRDs := CustomResourceDefinitionURLs{} | ||
err := json.Unmarshal(c.Config, &rcCRDs) | ||
if err != nil { | ||
return OrchestratorK8sCRDRemoteConfig{}, err | ||
} | ||
if rcCRDs.Crds != nil { | ||
crds = append(crds, *rcCRDs.Crds...) | ||
} | ||
} | ||
} | ||
|
||
if len(crds) == 0 { | ||
r.logger.Info("No CRDs received") | ||
return OrchestratorK8sCRDRemoteConfig{}, nil | ||
} | ||
|
||
// Merge configs | ||
var finalConfig OrchestratorK8sCRDRemoteConfig | ||
|
||
// Cleanup CRD duplicates and add to final config | ||
crds = removeDuplicateStr(crds) | ||
|
||
if finalConfig.CRDs == nil { | ||
finalConfig.CRDs = &CustomResourceDefinitionURLs{} | ||
} | ||
finalConfig.CRDs.Crds = &crds | ||
|
||
return finalConfig, nil | ||
} | ||
|
||
func (r *RemoteConfigUpdater) crdUpdateInstanceStatus(dda v2alpha1.DatadogAgent, config DatadogProductRemoteConfig) error { | ||
cfg, ok := config.(OrchestratorK8sCRDRemoteConfig) | ||
if !ok { | ||
return fmt.Errorf("invalid config type: %T", config) | ||
} | ||
|
||
newddaStatus := dda.Status.DeepCopy() | ||
if newddaStatus.RemoteConfigConfiguration == nil { | ||
newddaStatus.RemoteConfigConfiguration = &v2alpha1.RemoteConfigConfiguration{} | ||
} | ||
|
||
if newddaStatus.RemoteConfigConfiguration.Features == nil { | ||
newddaStatus.RemoteConfigConfiguration.Features = &v2alpha1.DatadogFeatures{} | ||
} | ||
|
||
if newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer == nil { | ||
newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer = &v2alpha1.OrchestratorExplorerFeatureConfig{} | ||
} | ||
|
||
// Orchestrator Explorer | ||
if cfg.CRDs != nil { | ||
newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer.CustomResources = []string{} | ||
// Overwrite custom resources by the new ones | ||
if cfg.CRDs.Crds != nil { | ||
newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer.CustomResources = *cfg.CRDs.Crds | ||
} | ||
newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer.CustomResources = removeDuplicateStr(newddaStatus.RemoteConfigConfiguration.Features.OrchestratorExplorer.CustomResources) | ||
} | ||
|
||
if !apiequality.Semantic.DeepEqual(&dda.Status, newddaStatus) { | ||
ddaUpdate := dda.DeepCopy() | ||
ddaUpdate.Status = *newddaStatus | ||
if err := r.kubeClient.Status().Update(context.TODO(), ddaUpdate); err != nil { | ||
if apierrors.IsConflict(err) { | ||
r.logger.Info("unable to update DatadogAgent CRD status due to update conflict") | ||
return nil | ||
} | ||
r.logger.Error(err, "unable to update DatadogAgent status") | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func removeDuplicateStr(s []string) []string { | ||
keys := make(map[string]bool) | ||
list := []string{} | ||
for _, item := range s { | ||
if _, value := keys[item]; !value { | ||
keys[item] = true | ||
list = append(list, item) | ||
} | ||
} | ||
return list | ||
} |
Oops, something went wrong.