1 回答

TA貢獻1887條經驗 獲得超5個贊
如果要將導入的類型作為接口返回,而這些類型尚未實現,則可以將它們包裝在實現它的類型中。
例如:
type K8sResource interface {
Name() string
Kind() string
OwnerReferences() []metav1.OwnerReference
}
type replicaSet struct{ *v1.ReplicaSet }
func (s replicaSet) Name() string {
return s.ReplicaSet.Name
}
func (s replicaSet) Kind() string {
return s.ReplicaSet.Kind
}
func (s replicaSet) OwnerReferences() []metav1.OwnerReference {
return s.ReplicaSet.OwnerReferences
}
func fetchResource(ref *metav1.OwnerReference, options *RequestOptions) (K8sResource, error) {
switch ref.Kind {
case "ReplicaSet":
res, err := options.Clientset.AppsV1().ReplicaSets(options.Namespace).Get(options.Context, ref.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return replicaSet{res}, nil // wrap it up
case "Pod":
res, err := options.Clientset.AppsV1().Pods(options.Namespace).Get(options.Context, ref.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return pod{res}, nil // wrap it up
case "Job":
fallthrough
// more stuff...
default:
return nil, nil
}
}
- 1 回答
- 0 關注
- 84 瀏覽
添加回答
舉報