Skip to content

Commit

Permalink
chore: refactor StaticPod and StaticPodStatus into typed.Resource
Browse files Browse the repository at this point in the history
This two required some additional attention and were split into separate branch. Also fix data race in NodeAddressSpec.DeepCopy method.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed May 4, 2022
1 parent d2935f9 commit 09d1634
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 77 deletions.
62 changes: 25 additions & 37 deletions pkg/machinery/resources/k8s/static_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,57 @@ package k8s
import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/resource/typed"
)

// StaticPodType is type of StaticPod resource.
const StaticPodType = resource.Type("StaticPods.kubernetes.talos.dev")

// StaticPod resource holds definition of kubelet static pod.
type StaticPod struct {
md resource.Metadata
spec *StaticPodSpec
}
type StaticPod = typed.Resource[StaticPodSpec, StaticPodRD]

// StaticPodSpec describes static pod spec, it contains marshaled *v1.Pod spec.
type StaticPodSpec struct {
Pod map[string]interface{}
}

// DeepCopy implements typed.DeepCopyable interface.
func (spec StaticPodSpec) DeepCopy() StaticPodSpec {
return StaticPodSpec{
Pod: copyMap(spec.Pod),
}
}

// MarshalYAML implements yaml.Marshaler.
func (spec *StaticPodSpec) MarshalYAML() (interface{}, error) {
func (spec StaticPodSpec) MarshalYAML() (interface{}, error) {
return spec.Pod, nil
}

// NewStaticPod initializes a StaticPod resource.
func NewStaticPod(namespace resource.Namespace, id resource.ID) *StaticPod {
r := &StaticPod{
md: resource.NewMetadata(namespace, StaticPodType, id, resource.VersionUndefined),
spec: &StaticPodSpec{},
}

r.md.BumpVersion()

return r
return typed.NewResource[StaticPodSpec, StaticPodRD](
resource.NewMetadata(namespace, StaticPodType, id, resource.VersionUndefined),
StaticPodSpec{},
)
}

// Metadata implements resource.Resource.
func (r *StaticPod) Metadata() *resource.Metadata {
return &r.md
}
// StaticPodRD provides auxiliary methods for StaticPod.
type StaticPodRD struct{}

// Spec implements resource.Resource.
func (r *StaticPod) Spec() interface{} {
return r.spec
}

// DeepCopy implements resource.Resource.
func (r *StaticPod) DeepCopy() resource.Resource {
return &StaticPod{
md: r.md,
spec: &StaticPodSpec{
Pod: r.spec.Pod,
},
}
}

// ResourceDefinition implements meta.ResourceDefinitionProvider interface.
func (r *StaticPod) ResourceDefinition() meta.ResourceDefinitionSpec {
// ResourceDefinition implements typed.ResourceDefinition interface.
func (StaticPodRD) ResourceDefinition(resource.Metadata, StaticPodSpec) meta.ResourceDefinitionSpec {
return meta.ResourceDefinitionSpec{
Type: StaticPodType,
Aliases: []resource.Type{},
DefaultNamespace: NamespaceName,
}
}

// TypedSpec returns .spec.
func (r *StaticPod) TypedSpec() *StaticPodSpec {
return r.spec
func copyMap[K comparable, V any](m map[K]V) map[K]V {
result := make(map[K]V, len(m))
for k, v := range m {
result[k] = v
}

return result
}
57 changes: 18 additions & 39 deletions pkg/machinery/resources/k8s/static_pod_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,45 @@ package k8s
import (
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/resource/typed"
)

// StaticPodStatusType is type of StaticPodStatus resource.
const StaticPodStatusType = resource.Type("StaticPodStatuses.kubernetes.talos.dev")

// StaticPodStatus resource holds definition of kubelet static pod.
type StaticPodStatus struct {
md resource.Metadata
spec *StaticPodStatusSpec
}
type StaticPodStatus = typed.Resource[StaticPodStatusSpec, StaticPodStatusRD]

// StaticPodStatusSpec describes kubelet static pod status.
type StaticPodStatusSpec struct {
PodStatus map[string]interface{}
}

// DeepCopy implements typed.DeepCopyable interface.
func (spec StaticPodStatusSpec) DeepCopy() StaticPodStatusSpec {
return StaticPodStatusSpec{
PodStatus: copyMap(spec.PodStatus),
}
}

// MarshalYAML implements yaml.Marshaler.
func (spec *StaticPodStatusSpec) MarshalYAML() (interface{}, error) {
func (spec StaticPodStatusSpec) MarshalYAML() (interface{}, error) {
return spec.PodStatus, nil
}

// NewStaticPodStatus initializes a StaticPodStatus resource.
func NewStaticPodStatus(namespace resource.Namespace, id resource.ID) *StaticPodStatus {
r := &StaticPodStatus{
md: resource.NewMetadata(namespace, StaticPodStatusType, id, resource.VersionUndefined),
spec: &StaticPodStatusSpec{},
}

r.md.BumpVersion()

return r
}

// Metadata implements resource.Resource.
func (r *StaticPodStatus) Metadata() *resource.Metadata {
return &r.md
}

// Spec implements resource.Resource.
func (r *StaticPodStatus) Spec() interface{} {
return r.spec
return typed.NewResource[StaticPodStatusSpec, StaticPodStatusRD](
resource.NewMetadata(namespace, StaticPodStatusType, id, resource.VersionUndefined),
StaticPodStatusSpec{},
)
}

// DeepCopy implements resource.Resource.
func (r *StaticPodStatus) DeepCopy() resource.Resource {
return &StaticPodStatus{
md: r.md,
spec: &StaticPodStatusSpec{
PodStatus: r.spec.PodStatus,
},
}
}
// StaticPodStatusRD provides auxiliary methods for StaticPodStatus.
type StaticPodStatusRD struct{}

// ResourceDefinition implements meta.ResourceDefinitionProvider interface.
func (r *StaticPodStatus) ResourceDefinition() meta.ResourceDefinitionSpec {
// ResourceDefinition implements typed.ResourceDefinition interface.
func (StaticPodStatusRD) ResourceDefinition(resource.Metadata, StaticPodStatusSpec) meta.ResourceDefinitionSpec {
return meta.ResourceDefinitionSpec{
Type: StaticPodStatusType,
Aliases: []resource.Type{"podstatus"},
Expand All @@ -74,8 +58,3 @@ func (r *StaticPodStatus) ResourceDefinition() meta.ResourceDefinitionSpec {
},
}
}

// TypedSpec returns .spec.
func (r *StaticPodStatus) TypedSpec() *StaticPodStatusSpec {
return r.spec
}
4 changes: 3 additions & 1 deletion pkg/machinery/resources/network/node_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ type NodeAddressSpec struct {

// DeepCopy generates a deep copy of NodeAddressSpec.
func (spec NodeAddressSpec) DeepCopy() NodeAddressSpec {
return spec
return NodeAddressSpec{
Addresses: append([]netaddr.IPPrefix(nil), spec.Addresses...),
}
}

// NewNodeAddress initializes a NodeAddress resource.
Expand Down

0 comments on commit 09d1634

Please sign in to comment.