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

issue 4837 - chore: main.go refactor #5110

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Changes from 4 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
6bb3949
Issue 4837: chore: Rename main_test.go to flags_test.go
mrajagopal Jan 31, 2024
0f05611
Merge branch 'nginxinc:main' into mrajagopal-issue-4837
mrajagopal Feb 4, 2024
beddad0
Merge branch 'nginxinc:main' into mrajagopal-issue-4837
mrajagopal Feb 15, 2024
39aa5f0
Issue 4837: chore: main.go refactor
mrajagopal Feb 16, 2024
f2b2873
Issue 4837: chore: main.go refactor
mrajagopal Feb 19, 2024
b95c264
Merge branch 'nginxinc:main' into mrajagopal-issue-4837
mrajagopal Feb 19, 2024
fb29d9a
Issue 4837: chore: main.go refactor
mrajagopal Feb 19, 2024
4de9b37
Issue 4837: chore: main.go refactor
mrajagopal Feb 26, 2024
e139372
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Feb 26, 2024
8ae17f9
Issue 4837: chore: main.go refactor
mrajagopal Feb 26, 2024
7b88141
chore: This is pre-commit install
mrajagopal Feb 26, 2024
8a0f5d8
Update cmd/nginx-ingress/main.go
mrajagopal Feb 26, 2024
2162fd7
Update cmd/nginx-ingress/main.go
mrajagopal Feb 26, 2024
4b456e6
Issue 4837: chore: main.go refactor
mrajagopal Feb 26, 2024
668c7d3
Issue 4837: chore: main.go refactor
mrajagopal Feb 27, 2024
e9e37e9
Issue 4837: chore: main.go refactor
mrajagopal Feb 27, 2024
a60f7e2
Issue 4837: chore: main.go refactor
mrajagopal Feb 28, 2024
05736e3
Issue 4837: chore: main.go refactor
mrajagopal Feb 28, 2024
88cfd23
Issue 4837: chore: main.go refactor
mrajagopal Feb 28, 2024
fa3dfc9
Issue 4837: chore: main_test.go unit-tests
mrajagopal Feb 28, 2024
eb72565
Issue 4837: chore: main_test.go unit-tests
mrajagopal Feb 28, 2024
d37e3bb
Issue 4837: chore: main_test.go unit-tests
mrajagopal Feb 28, 2024
9b17488
Issue 4837: chore: main_test.go unit-tests
mrajagopal Feb 29, 2024
86e7522
Issue 4837: chore: main_test.go unit-tests
mrajagopal Mar 5, 2024
ffd5e52
Issue 4837: chore: main_test.go unit-tests
mrajagopal Mar 5, 2024
2dec987
Issue 4837: chore: main_test.go unit-tests
mrajagopal Mar 8, 2024
27d3e01
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Mar 8, 2024
e9e8a2b
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 22, 2024
d3bf5ac
Issue 4837: chore: main_test.go unit-tests
mrajagopal Apr 23, 2024
5d26b05
Issue 4837: chore: main_test.go unit-tests
mrajagopal Apr 24, 2024
b6aacea
Issue 4837: chore: main_test.go unit-tests
mrajagopal Apr 24, 2024
829cf3b
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 24, 2024
2e3b85a
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 25, 2024
345fff2
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 29, 2024
d848e6f
Merge branch 'main' into mrajagopal-issue-4837
vepatel Apr 29, 2024
ef3e788
Merge branch 'main' into mrajagopal-issue-4837
vepatel Apr 29, 2024
9d56606
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 29, 2024
1ee9d4b
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Apr 30, 2024
13cd17b
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal May 1, 2024
8567a45
Issue 4837: chore: main_test.go unit-tests
mrajagopal May 2, 2024
513a8b1
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal May 2, 2024
61f3a2c
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal May 15, 2024
2a273f8
Merge branch 'main' into mrajagopal-issue-4837
mrajagopal Jun 17, 2024
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
98 changes: 56 additions & 42 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,25 @@

parseFlags()

config, kubeClient := createConfigAndKubeClient()
config, err := getClientConfig()
if err != nil {
glog.Fatalf("error creating client configuration: %v", err)
}

kubeClient, err := getKubeClient(config)
if err != nil {
glog.Fatalf("Failed to create client: %v.", err)
}

kubernetesVersionInfo(kubeClient)
err = confirmMinimumkubernetesVersionCriteria(kubeClient)
if err != nil {
glog.Fatal(err)
}

validateIngressClass(kubeClient)
err = validateIngressClass(kubeClient)
if err != nil {
glog.Fatal(err)
}

checkNamespaces(kubeClient)

Expand Down Expand Up @@ -133,7 +147,10 @@
nginxDone := make(chan error, 1)
nginxManager.Start(nginxDone)

plusClient := createPlusClient(*nginxPlus, useFakeNginxManager, nginxManager)
plusClient, err := createPlusClient(*nginxPlus, useFakeNginxManager, nginxManager)
if err != nil {
glog.Fatal(err)
}

plusCollector, syslogListener, latencyCollector := createPlusAndLatencyCollectors(registry, constLabels, kubeClient, plusClient, staticCfgParams.NginxServiceMesh)
cnf := configs.NewConfigurator(configs.ConfiguratorParams{
Expand Down Expand Up @@ -230,9 +247,7 @@
}
}

func createConfigAndKubeClient() (*rest.Config, *kubernetes.Clientset) {
var config *rest.Config
var err error
func getClientConfig() (config *rest.Config, err error) {
if *proxyURL != "" {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{},
Expand All @@ -241,70 +256,72 @@
Server: *proxyURL,
},
}).ClientConfig()
if err != nil {
glog.Fatalf("error creating client configuration: %v", err)
}
} else {
if config, err = rest.InClusterConfig(); err != nil {
glog.Fatalf("error creating client configuration: %v", err)
}
config, err = rest.InClusterConfig()
}

kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
glog.Fatalf("Failed to create client: %v.", err)
}
return config, err
}

return config, kubeClient
func getKubeClient(config *rest.Config) (kubeClient *kubernetes.Clientset, err error) {
kubeClient, err = kubernetes.NewForConfig(config)
return kubeClient, err
}

func kubernetesVersionInfo(kubeClient kubernetes.Interface) {
func confirmMinimumkubernetesVersionCriteria(kubeClient kubernetes.Interface) (err error) {
mrajagopal marked this conversation as resolved.
Show resolved Hide resolved
k8sVersion, err := k8s.GetK8sVersion(kubeClient)
if err != nil {
glog.Fatalf("error retrieving k8s version: %v", err)
return fmt.Errorf("error retrieving k8s version: %v", err)

Check failure on line 274 in cmd/nginx-ingress/main.go

View workflow job for this annotation

GitHub Actions / Lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
glog.Infof("Kubernetes version: %v", k8sVersion)

minK8sVersion, err := util_version.ParseGeneric("1.22.0")
if err != nil {
glog.Fatalf("unexpected error parsing minimum supported version: %v", err)
return fmt.Errorf("unexpected error parsing minimum supported version: %v", err)

Check failure on line 280 in cmd/nginx-ingress/main.go

View workflow job for this annotation

GitHub Actions / Lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

if !k8sVersion.AtLeast(minK8sVersion) {
glog.Fatalf("Versions of Kubernetes < %v are not supported, please refer to the documentation for details on supported versions and legacy controller support.", minK8sVersion)
return fmt.Errorf("Versions of Kubernetes < %v are not supported, please refer to the documentation for details on supported versions and legacy controller support", minK8sVersion)
}
return err
}

func validateIngressClass(kubeClient kubernetes.Interface) {
func validateIngressClass(kubeClient kubernetes.Interface) (err error) {
ingressClassRes, err := kubeClient.NetworkingV1().IngressClasses().Get(context.TODO(), *ingressClass, meta_v1.GetOptions{})
if err != nil {
glog.Fatalf("Error when getting IngressClass %v: %v", *ingressClass, err)
return fmt.Errorf("Error when getting IngressClass %v: %v", *ingressClass, err)

Check failure on line 292 in cmd/nginx-ingress/main.go

View workflow job for this annotation

GitHub Actions / Lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

if ingressClassRes.Spec.Controller != k8s.IngressControllerName {
glog.Fatalf("IngressClass with name %v has an invalid Spec.Controller %v; expected %v", ingressClassRes.Name, ingressClassRes.Spec.Controller, k8s.IngressControllerName)
return fmt.Errorf("IngressClass with name %v has an invalid Spec.Controller %v; expected %v", ingressClassRes.Name, ingressClassRes.Spec.Controller, k8s.IngressControllerName)
}

return err
}

func checkNamespaces(kubeClient kubernetes.Interface) {
if *watchNamespaceLabel != "" {
// bootstrap the watched namespace list
var newWatchNamespaces []string
nsList, err := kubeClient.CoreV1().Namespaces().List(context.TODO(), meta_v1.ListOptions{LabelSelector: *watchNamespaceLabel})
if err != nil {
glog.Errorf("error when getting Namespaces with the label selector %v: %v", watchNamespaceLabel, err)
}
for _, ns := range nsList.Items {
newWatchNamespaces = append(newWatchNamespaces, ns.Name)
}
watchNamespaces = newWatchNamespaces
glog.Infof("Namespaces watched using label %v: %v", *watchNamespaceLabel, watchNamespaces)
watchNamespaces = getWatchedNamespaces(kubeClient)
} else {
checkNamespaceExists(kubeClient, watchNamespaces)
}
checkNamespaceExists(kubeClient, watchSecretNamespaces)
}

func getWatchedNamespaces(kubeClient kubernetes.Interface) (newWatchNamespaces []string) {
// bootstrap the watched namespace list
nsList, err := kubeClient.CoreV1().Namespaces().List(context.TODO(), meta_v1.ListOptions{LabelSelector: *watchNamespaceLabel})
if err != nil {
glog.Errorf("error when getting Namespaces with the label selector %v: %v", watchNamespaceLabel, err)
}
for _, ns := range nsList.Items {
newWatchNamespaces = append(newWatchNamespaces, ns.Name)
}
glog.Infof("Namespaces watched using label %v: %v", *watchNamespaceLabel, watchNamespaces)

return newWatchNamespaces
}

func checkNamespaceExists(kubeClient kubernetes.Interface, namespaces []string) {
for _, ns := range namespaces {
if ns != "" {
Expand Down Expand Up @@ -341,19 +358,16 @@
return dynClient, confClient
}

func createPlusClient(nginxPlus bool, useFakeNginxManager bool, nginxManager nginx.Manager) *client.NginxClient {
var plusClient *client.NginxClient
var err error

func createPlusClient(nginxPlus bool, useFakeNginxManager bool, nginxManager nginx.Manager) (plusClient *client.NginxClient, err error) {
if nginxPlus && !useFakeNginxManager {
httpClient := getSocketClient("/var/lib/nginx/nginx-plus-api.sock")
plusClient, err = client.NewNginxClient("http://nginx-plus-api/api", client.WithHTTPClient(httpClient))
if err != nil {
glog.Fatalf("Failed to create NginxClient for Plus: %v", err)
return plusClient, fmt.Errorf("Failed to create NginxClient for Plus: %v", err)

Check failure on line 366 in cmd/nginx-ingress/main.go

View workflow job for this annotation

GitHub Actions / Lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
nginxManager.SetPlusClients(plusClient, httpClient)
}
return plusClient
return plusClient, nil
}

func createTemplateExecutors() (*version1.TemplateExecutor, *version2.TemplateExecutor) {
Expand Down
Loading