-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Kubernetes v1.18.9 dependencies (#583)
Signed-off-by: 1gtm <[email protected]>
- Loading branch information
Showing
8 changed files
with
191 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 v1 | ||
|
||
import ( | ||
core "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
PodConditionTypeReady = core.PodConditionType("kubedb.com/Ready") | ||
) | ||
|
||
// HasCondition returns "true" if the desired condition provided in "condType" is present in the condition list. | ||
// Otherwise, it returns "false". | ||
func HasPodCondition(conditions []core.PodCondition, condType core.PodConditionType) bool { | ||
for i := range conditions { | ||
if conditions[i].Type == condType { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// GetPodCondition returns a pointer to the desired condition referred by "condType". Otherwise, it returns nil. | ||
func GetPodCondition(conditions []core.PodCondition, condType core.PodConditionType) (int, *core.PodCondition) { | ||
for i := range conditions { | ||
c := conditions[i] | ||
if c.Type == condType { | ||
return i, &c | ||
} | ||
} | ||
return -1, nil | ||
} | ||
|
||
// SetPodCondition add/update the desired condition to the condition list. It does nothing if the condition is already in | ||
// its desired state. | ||
func SetPodCondition(conditions []core.PodCondition, newCondition core.PodCondition) []core.PodCondition { | ||
idx, curCond := GetPodCondition(conditions, newCondition.Type) | ||
// The desired conditions is not in the condition list or is not in its desired state. | ||
// If the current condition status is in its desired state, we have nothing to do. Just return the original condition list. | ||
// Update it if present in the condition list, or append the new condition if it does not present. | ||
if curCond == nil || idx == -1 { | ||
return append(conditions, newCondition) | ||
} else if curCond.Status == newCondition.Status { | ||
return conditions | ||
} else if curCond.Status != newCondition.Status { | ||
conditions[idx].Status = newCondition.Status | ||
conditions[idx].LastTransitionTime = newCondition.LastTransitionTime | ||
conditions[idx].Reason = newCondition.Reason | ||
conditions[idx].Message = newCondition.Message | ||
} | ||
return conditions | ||
} | ||
|
||
// RemovePodCondition remove a condition from the condition list referred by "condType" parameter. | ||
func RemovePodCondition(conditions []core.PodCondition, condType core.PodConditionType) []core.PodCondition { | ||
idx, _ := GetPodCondition(conditions, condType) | ||
if idx == -1 { | ||
// The desired condition is not present in the condition list. So, nothing to do. | ||
return conditions | ||
} | ||
return append(conditions[:idx], conditions[idx+1:]...) | ||
} | ||
|
||
// IsPodConditionTrue returns "true" if the desired condition is in true state. | ||
// It returns "false" if the desired condition is not in "true" state or is not in the condition list. | ||
func IsPodConditionTrue(conditions []core.PodCondition, condType core.PodConditionType) bool { | ||
for i := range conditions { | ||
if conditions[i].Type == condType && conditions[i].Status == core.ConditionTrue { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// IsPodConditionFalse returns "true" if the desired condition is in false state. | ||
// It returns "false" if the desired condition is not in "false" state or is not in the condition list. | ||
func IsPodConditionFalse(conditions []core.PodCondition, condType core.PodConditionType) bool { | ||
for i := range conditions { | ||
if conditions[i].Type == condType && conditions[i].Status == core.ConditionFalse { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func UpsertPodReadinessGateConditionType(readinessGates []core.PodReadinessGate, conditionType core.PodConditionType) []core.PodReadinessGate { | ||
for i := range readinessGates { | ||
if readinessGates[i].ConditionType == conditionType { | ||
return readinessGates | ||
} | ||
} | ||
return append(readinessGates, core.PodReadinessGate{ | ||
ConditionType: conditionType, | ||
}) | ||
} |
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,50 @@ | ||
package meta | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func DecodeObject(in map[string]interface{}, out interface{}) error { | ||
config := &mapstructure.DecoderConfig{ | ||
Metadata: nil, | ||
TagName: "json", | ||
Result: out, | ||
} | ||
decoder, err := mapstructure.NewDecoder(config) | ||
if err != nil { | ||
return err | ||
} | ||
return decoder.Decode(in) | ||
} | ||
|
||
type DataFormat string | ||
|
||
const ( | ||
// Do not change format | ||
KeepFormat DataFormat = "" | ||
JsonFormat DataFormat = "json" | ||
YAMLFormat DataFormat = "yaml" | ||
) | ||
|
||
func NewDataFormat(format string, def DataFormat) DataFormat { | ||
switch format { | ||
case string(YAMLFormat): | ||
return YAMLFormat | ||
case string(JsonFormat): | ||
return JsonFormat | ||
default: | ||
return def | ||
} | ||
} | ||
|
||
func Marshal(v interface{}, format DataFormat) ([]byte, error) { | ||
if format == JsonFormat { | ||
return json.Marshal(v) | ||
} else if format == YAMLFormat { | ||
return yaml.Marshal(v) | ||
} | ||
return nil, fmt.Errorf("unknonw format: %v", format) | ||
} |
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