Skip to content

Commit e5defb3

Browse files
Arta AsadiArta Asadi
authored andcommitted
fix: have most of the structs
1 parent 7a63657 commit e5defb3

File tree

11 files changed

+6074
-381
lines changed

11 files changed

+6074
-381
lines changed

discovery/describers/kubernetes.go

Lines changed: 54 additions & 47 deletions
Large diffs are not rendered by default.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package helpers
2+
3+
import (
4+
rbacv1 "k8s.io/api/rbac/v1"
5+
)
6+
7+
// --- ClusterRole ---
8+
type ClusterRole struct {
9+
TypeMeta
10+
ObjectMeta
11+
Rules []PolicyRule
12+
AggregationRule *AggregationRule
13+
}
14+
15+
// ConvertClusterRole creates a helper ClusterRole from a rbacv1 ClusterRole
16+
func ConvertClusterRole(cr *rbacv1.ClusterRole) ClusterRole {
17+
return ClusterRole{
18+
TypeMeta: ConvertTypeMeta(cr.TypeMeta),
19+
ObjectMeta: ConvertObjectMeta(&cr.ObjectMeta),
20+
Rules: ConvertPolicyRules(cr.Rules),
21+
AggregationRule: ConvertAggregationRule(cr.AggregationRule),
22+
}
23+
}
24+
25+
// --- PolicyRule ---
26+
type PolicyRule struct {
27+
Verbs []string
28+
APIGroups []string
29+
Resources []string
30+
ResourceNames []string
31+
NonResourceURLs []string
32+
}
33+
34+
func ConvertPolicyRules(policyRules []rbacv1.PolicyRule) []PolicyRule {
35+
if policyRules == nil {
36+
return nil
37+
}
38+
rules := make([]PolicyRule, len(policyRules))
39+
for i, policyRule := range policyRules {
40+
rules[i] = PolicyRule{
41+
Verbs: policyRule.Verbs,
42+
APIGroups: policyRule.APIGroups,
43+
Resources: policyRule.Resources,
44+
ResourceNames: policyRule.ResourceNames,
45+
NonResourceURLs: policyRule.NonResourceURLs,
46+
}
47+
}
48+
return rules
49+
}
50+
51+
// --- AggregationRule ---
52+
type AggregationRule struct {
53+
ClusterRoleSelectors []LabelSelector // Assumes LabelSelector in model_helpers.go
54+
}
55+
56+
func ConvertAggregationRule(aggregationRule *rbacv1.AggregationRule) *AggregationRule {
57+
if aggregationRule == nil {
58+
return nil
59+
}
60+
selectors := make([]LabelSelector, 0, len(aggregationRule.ClusterRoleSelectors))
61+
for _, sel := range aggregationRule.ClusterRoleSelectors {
62+
// Assuming ConvertLabelSelector exists in model_helpers.go
63+
convertedSel := ConvertLabelSelector(&sel)
64+
if convertedSel != nil {
65+
selectors = append(selectors, *convertedSel)
66+
}
67+
}
68+
return &AggregationRule{
69+
ClusterRoleSelectors: selectors,
70+
}
71+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package helpers
2+
3+
import (
4+
rbacv1 "k8s.io/api/rbac/v1"
5+
)
6+
7+
// --- ClusterRoleBinding ---
8+
type ClusterRoleBinding struct {
9+
TypeMeta
10+
ObjectMeta
11+
Subjects []Subject // Uses Subject from this file
12+
RoleRef RoleRef // Uses RoleRef from this file
13+
}
14+
15+
// ConvertClusterRoleBinding creates a helper ClusterRoleBinding from a rbacv1 ClusterRoleBinding
16+
func ConvertClusterRoleBinding(crb *rbacv1.ClusterRoleBinding) ClusterRoleBinding {
17+
return ClusterRoleBinding{
18+
TypeMeta: ConvertTypeMeta(crb.TypeMeta), // Assumes ConvertTypeMeta in model_helpers.go
19+
ObjectMeta: ConvertObjectMeta(&crb.ObjectMeta), // Assumes ConvertObjectMeta in model_helpers.go
20+
Subjects: ConvertSubjects(crb.Subjects), // Uses ConvertSubjects from this file
21+
RoleRef: ConvertRoleRef(crb.RoleRef), // Uses ConvertRoleRef from this file
22+
}
23+
}
24+
25+
// --- Subject ---
26+
type Subject struct {
27+
Kind string
28+
APIGroup string
29+
Name string
30+
Namespace string
31+
}
32+
33+
// ConvertSubject converts a single rbacv1.Subject to a helper Subject
34+
func ConvertSubject(srcSubject rbacv1.Subject) Subject {
35+
return Subject{
36+
Kind: srcSubject.Kind,
37+
APIGroup: srcSubject.APIGroup,
38+
Name: srcSubject.Name,
39+
Namespace: srcSubject.Namespace,
40+
}
41+
}
42+
43+
// ConvertSubjects converts a slice of rbacv1.Subject to a slice of helper Subject
44+
func ConvertSubjects(srcSubjects []rbacv1.Subject) []Subject {
45+
if srcSubjects == nil {
46+
return nil
47+
}
48+
subjects := make([]Subject, len(srcSubjects))
49+
for i, srcSubject := range srcSubjects {
50+
subjects[i] = ConvertSubject(srcSubject)
51+
}
52+
return subjects
53+
}
54+
55+
// --- RoleRef ---
56+
type RoleRef struct {
57+
APIGroup string
58+
Kind string
59+
Name string
60+
}
61+
62+
// ConvertRoleRef converts a rbacv1.RoleRef to a helper RoleRef
63+
func ConvertRoleRef(roleRef rbacv1.RoleRef) RoleRef {
64+
return RoleRef{
65+
APIGroup: roleRef.APIGroup,
66+
Kind: roleRef.Kind,
67+
Name: roleRef.Name,
68+
}
69+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package helpers
2+
3+
import (
4+
corev1 "k8s.io/api/core/v1"
5+
)
6+
7+
// --- ConfigMap ---
8+
type ConfigMap struct {
9+
TypeMeta // Assumes TypeMeta in model_helpers.go
10+
ObjectMeta // Assumes ObjectMeta in model_helpers.go
11+
Immutable *bool
12+
Data map[string]string
13+
BinaryData map[string][]byte
14+
}
15+
16+
// ConvertConfigMap creates a helper ConfigMap from a corev1 ConfigMap
17+
func ConvertConfigMap(cm *corev1.ConfigMap) ConfigMap {
18+
return ConfigMap{
19+
TypeMeta: ConvertTypeMeta(cm.TypeMeta), // Assumes ConvertTypeMeta in model_helpers.go
20+
ObjectMeta: ConvertObjectMeta(&cm.ObjectMeta), // Assumes ConvertObjectMeta in model_helpers.go
21+
Immutable: cm.Immutable,
22+
Data: cm.Data,
23+
BinaryData: cm.BinaryData,
24+
}
25+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package helpers
2+
3+
import (
4+
"time"
5+
6+
batchv1 "k8s.io/api/batch/v1"
7+
corev1 "k8s.io/api/core/v1"
8+
)
9+
10+
type JobTemplateSpec struct {
11+
ObjectMeta
12+
Spec JobSpec
13+
}
14+
15+
func ConvertJobTemplateSpec(spec batchv1.JobTemplateSpec) JobTemplateSpec {
16+
return JobTemplateSpec{
17+
ObjectMeta: ConvertObjectMeta(&spec.ObjectMeta),
18+
Spec: ConvertJobSpec(spec.Spec),
19+
}
20+
}
21+
22+
type CronJobSpec struct {
23+
Schedule string
24+
TimeZone *string
25+
StartingDeadlineSeconds *int64
26+
ConcurrencyPolicy string // batchv1.ConcurrencyPolicy
27+
Suspend *bool
28+
JobTemplate JobTemplateSpec
29+
SuccessfulJobsHistoryLimit *int32
30+
FailedJobsHistoryLimit *int32
31+
}
32+
33+
func ConvertConcurrencyPolicy(cp batchv1.ConcurrencyPolicy) string {
34+
return string(cp)
35+
}
36+
37+
func ConvertCronJobSpec(spec batchv1.CronJobSpec) CronJobSpec {
38+
return CronJobSpec{
39+
Schedule: spec.Schedule,
40+
TimeZone: spec.TimeZone,
41+
StartingDeadlineSeconds: spec.StartingDeadlineSeconds,
42+
ConcurrencyPolicy: ConvertConcurrencyPolicy(spec.ConcurrencyPolicy),
43+
Suspend: spec.Suspend,
44+
JobTemplate: ConvertJobTemplateSpec(spec.JobTemplate),
45+
SuccessfulJobsHistoryLimit: spec.SuccessfulJobsHistoryLimit,
46+
FailedJobsHistoryLimit: spec.FailedJobsHistoryLimit,
47+
}
48+
}
49+
50+
func ConvertObjectReferences(objReferences []corev1.ObjectReference) []ObjectReference {
51+
references := make([]ObjectReference, len(objReferences))
52+
for i, objReference := range objReferences {
53+
references[i] = ObjectReference{
54+
Kind: objReference.Kind,
55+
Namespace: objReference.Namespace,
56+
Name: objReference.Name,
57+
UID: objReference.UID,
58+
APIVersion: objReference.APIVersion,
59+
ResourceVersion: objReference.ResourceVersion,
60+
FieldPath: objReference.FieldPath,
61+
}
62+
}
63+
return references
64+
}
65+
66+
type CronJobStatus struct {
67+
Active []ObjectReference
68+
LastScheduleTime *time.Time
69+
LastSuccessfulTime *time.Time
70+
}
71+
72+
func ConvertCronJobStatus(status batchv1.CronJobStatus) CronJobStatus {
73+
return CronJobStatus{
74+
Active: ConvertObjectReferences(status.Active),
75+
LastScheduleTime: ConvertTimePtr(status.LastScheduleTime),
76+
LastSuccessfulTime: ConvertTimePtr(status.LastSuccessfulTime),
77+
}
78+
}
79+
80+
type CronJob struct {
81+
TypeMeta
82+
ObjectMeta
83+
Spec CronJobSpec
84+
Status CronJobStatus
85+
}
86+
87+
func ConvertCronJob(cj *batchv1.CronJob) CronJob {
88+
return CronJob{
89+
TypeMeta: ConvertTypeMeta(cj.TypeMeta),
90+
ObjectMeta: ConvertObjectMeta(&cj.ObjectMeta),
91+
Spec: ConvertCronJobSpec(cj.Spec),
92+
Status: ConvertCronJobStatus(cj.Status),
93+
}
94+
}

0 commit comments

Comments
 (0)