-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: prevent collector pod restarts on startup
- Loading branch information
1 parent
a1ad2b0
commit 0c59776
Showing
14 changed files
with
523 additions
and
59 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
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,43 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 operator | ||
|
||
import "github.com/GoogleCloudPlatform/prometheus-engine/pkg/export" | ||
|
||
// resolveLabels compares the project, location, and cluster labels used by the operator | ||
// against those in externalLabels. If any are found in the latter, they take precedence. | ||
// The higher-precedence labels are then returned. | ||
// | ||
// This is to be consistent with our export layer's priorities and avoid confusion if users | ||
// specify a project_id, location, and cluster in the OperatorConfig's external labels but | ||
// not in flags passed to the operator - since on GKE environments, these values are | ||
// auto-populated without user intervention. | ||
func resolveLabels(defaultProjectID, defaultLocation, defaultCluster string, externalLabels map[string]string) (projectID, location, cluster string) { | ||
if externalLabels == nil { | ||
return defaultProjectID, defaultLocation, defaultCluster | ||
} | ||
|
||
var ok bool | ||
if projectID, ok = externalLabels[export.KeyProjectID]; !ok { | ||
projectID = defaultProjectID | ||
} | ||
if location, ok = externalLabels[export.KeyLocation]; !ok { | ||
location = defaultLocation | ||
} | ||
if cluster, ok = externalLabels[export.KeyCluster]; !ok { | ||
cluster = defaultCluster | ||
} | ||
return | ||
} |
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,106 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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 operator | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestResolveLabels(t *testing.T) { | ||
for _, tc := range []struct { | ||
desc string | ||
projectID, location, cluster string | ||
expProjectID, expLocation, expCluster string | ||
externalLabels map[string]string | ||
expExternalLabels map[string]string | ||
}{ | ||
{ | ||
desc: "no overwrite", | ||
projectID: "proj-a", | ||
location: "loc-a", | ||
cluster: "clu-a", | ||
externalLabels: map[string]string{ | ||
"project_id": "proj-b", | ||
"location": "loc-b", | ||
"cluster": "clu-b", | ||
}, | ||
expProjectID: "proj-b", | ||
expLocation: "loc-b", | ||
expCluster: "clu-b", | ||
}, | ||
{ | ||
desc: "overwrite projectID", | ||
projectID: "proj-a", | ||
location: "loc-a", | ||
cluster: "clu-a", | ||
externalLabels: map[string]string{ | ||
"location": "loc-b", | ||
"cluster": "clu-b", | ||
}, | ||
expProjectID: "proj-a", | ||
expLocation: "loc-b", | ||
expCluster: "clu-b", | ||
}, | ||
{ | ||
desc: "overwrite location", | ||
projectID: "proj-a", | ||
location: "loc-a", | ||
cluster: "clu-a", | ||
externalLabels: map[string]string{ | ||
"project_id": "proj-b", | ||
"cluster": "clu-b", | ||
}, | ||
expProjectID: "proj-b", | ||
expLocation: "loc-a", | ||
expCluster: "clu-b", | ||
}, | ||
{ | ||
desc: "overwrite cluster", | ||
projectID: "proj-a", | ||
location: "loc-a", | ||
cluster: "clu-a", | ||
externalLabels: map[string]string{ | ||
"project_id": "proj-b", | ||
"location": "loc-b", | ||
}, | ||
expProjectID: "proj-b", | ||
expLocation: "loc-b", | ||
expCluster: "clu-a", | ||
}, | ||
{ | ||
desc: "overwrite all", | ||
projectID: "proj-a", | ||
location: "loc-a", | ||
cluster: "clu-a", | ||
externalLabels: map[string]string{}, | ||
expProjectID: "proj-a", | ||
expLocation: "loc-a", | ||
expCluster: "clu-a", | ||
}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
projectID, location, cluster := resolveLabels(tc.projectID, tc.location, tc.cluster, tc.externalLabels) | ||
if projectID != tc.expProjectID { | ||
t.Error("projectIDs do not match") | ||
} | ||
if location != tc.expLocation { | ||
t.Error("locations do not match") | ||
} | ||
if cluster != tc.expCluster { | ||
t.Error("clusters do not match") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.