forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cpu/memory funcs for k8s (#21)
* feat: add cpu/memory funcs for k8s * chore: fix lint + tests * chore: use lower case for k8s memory conversion * chore: add tests for lowercase memory
- Loading branch information
1 parent
41f8577
commit dd952f5
Showing
3 changed files
with
136 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package celext | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/flanksource/gomplate/v3/conv" | ||
"github.com/flanksource/gomplate/v3/k8s" | ||
"github.com/google/cel-go/cel" | ||
"github.com/google/cel-go/common/types" | ||
"github.com/google/cel-go/common/types/ref" | ||
) | ||
|
||
func k8sHealth() cel.EnvOption { | ||
return cel.Function("k8s.health", | ||
cel.Overload("k8s.health_any", | ||
[]*cel.Type{cel.AnyType}, | ||
cel.AnyType, | ||
cel.UnaryBinding(func(obj ref.Val) ref.Val { | ||
jsonObj, _ := anyToMapStringAny(k8s.GetHealth(obj.Value())) | ||
return types.NewDynamicMap(types.DefaultTypeAdapter, jsonObj) | ||
}), | ||
), | ||
) | ||
} | ||
|
||
func k8sIsHealthy() cel.EnvOption { | ||
return cel.Function("k8s.is_healthy", | ||
cel.Overload("k8s.is_healthy_any", | ||
[]*cel.Type{cel.AnyType}, | ||
cel.StringType, | ||
cel.UnaryBinding(func(obj ref.Val) ref.Val { | ||
return types.Bool(k8s.GetHealth(obj.Value()).OK) | ||
}), | ||
), | ||
) | ||
} | ||
|
||
func k8sCPUAsMillicores() cel.EnvOption { | ||
return cel.Function("k8s.cpuAsMillicores", | ||
cel.Overload("k8s.cpuAsMillicores_string", | ||
[]*cel.Type{cel.StringType}, | ||
cel.IntType, | ||
cel.UnaryBinding(func(obj ref.Val) ref.Val { | ||
objVal := conv.ToString(obj.Value()) | ||
var cpu int64 | ||
if strings.HasSuffix(objVal, "m") { | ||
cpu = conv.ToInt64(strings.ReplaceAll(objVal, "m", "")) | ||
} else { | ||
cpu = int64(conv.ToFloat64(objVal) * 1000) | ||
} | ||
return types.Int(cpu) | ||
}), | ||
), | ||
) | ||
} | ||
|
||
func k8sMemoryAsBytes() cel.EnvOption { | ||
return cel.Function("k8s.memoryAsBytes", | ||
cel.Overload("k8s.memoryAsBytes_string", | ||
[]*cel.Type{cel.StringType}, | ||
cel.IntType, | ||
cel.UnaryBinding(func(obj ref.Val) ref.Val { | ||
objVal := strings.ToLower(conv.ToString(obj.Value())) | ||
var memory int64 | ||
switch { | ||
case strings.HasSuffix(objVal, "gi"): | ||
memory = int64(conv.ToFloat64(strings.ReplaceAll(objVal, "gi", "")) * 1024 * 1024 * 1024) | ||
case strings.HasSuffix(objVal, "mi"): | ||
memory = int64(conv.ToFloat64(strings.ReplaceAll(objVal, "mi", "")) * 1024 * 1024) | ||
case strings.HasSuffix(objVal, "ki"): | ||
memory = int64(conv.ToFloat64(strings.ReplaceAll(objVal, "ki", "")) * 1024) | ||
default: | ||
memory = conv.ToInt64(objVal) | ||
} | ||
|
||
return types.Int(memory) | ||
}), | ||
), | ||
) | ||
} |