Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator/pkg: mitigate null pointer dereference in Validate() and newRunData() functions #5617

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions operator/pkg/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func (opt *InitOptions) Validate() error {
return fmt.Errorf("unexpected invalid crds remote url %s", opt.CRDTarball.HTTPSource.URL)
}
}
if opt.Karmada == nil || opt.Karmada.Spec.Components == nil || opt.Karmada.Spec.Components.KarmadaAPIServer == nil {
return fmt.Errorf("invalid Karmada configuration: Karmada, Karmada components, and Karmada API server must be defined")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt.Karmada.Spec.Components == nil

I'm curious why components are not allowed to be nil. From the API, it seems all fields are optional.
Do you know some background? @chaosi-zju

if !util.IsInCluster(opt.Karmada.Spec.HostCluster) && opt.Karmada.Spec.Components.KarmadaAPIServer.ServiceType == corev1.ServiceTypeClusterIP {
return fmt.Errorf("if karmada is installed in a remote cluster, the service type of karmada-apiserver must be either NodePort or LoadBalancer")
}
Expand All @@ -73,7 +76,7 @@ func (opt *InitOptions) Validate() error {
return fmt.Errorf("unexpected karmada invalid version %s", opt.KarmadaVersion)
}

if opt.Karmada.Spec.Components.Etcd.Local != nil && opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas != nil {
if opt.Karmada.Spec.Components.Etcd != nil && opt.Karmada.Spec.Components.Etcd.Local != nil && opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas != nil {
replicas := *opt.Karmada.Spec.Components.Etcd.Local.CommonSettings.Replicas

if (replicas % 2) == 0 {
Expand Down Expand Up @@ -177,6 +180,11 @@ func newRunData(opt *InitOptions) (*initData, error) {
}
}

var hostClusterDNSDomain string
if opt.Karmada.Spec.HostCluster != nil && opt.Karmada.Spec.HostCluster.Networking != nil && opt.Karmada.Spec.HostCluster.Networking.DNSDomain != nil {
hostClusterDNSDomain = *opt.Karmada.Spec.HostCluster.Networking.DNSDomain
}

return &initData{
name: opt.Name,
namespace: opt.Namespace,
Expand All @@ -188,7 +196,7 @@ func newRunData(opt *InitOptions) (*initData, error) {
privateRegistry: privateRegistry,
components: opt.Karmada.Spec.Components,
featureGates: opt.Karmada.Spec.FeatureGates,
dnsDomain: *opt.Karmada.Spec.HostCluster.Networking.DNSDomain,
dnsDomain: hostClusterDNSDomain,
CertStore: certs.NewCertStore(),
}, nil
}
Expand Down