-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
replacing hard coded cluster.local with variable #2892
Changes from 4 commits
32be14b
3a246e0
cad09ac
060bba4
8a4fc86
5ebef41
bd02e4d
2a99021
f5401cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2018 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
resolverFileName = "/etc/resolv.conf" | ||
) | ||
|
||
// GetClusterDomainName returns cluster's domain name or an error | ||
// Closes issue: https://github.com/knative/eventing/issues/714 | ||
func GetClusterDomainName() (string, error) { | ||
f, err := os.Open(resolverFileName) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer f.Close() | ||
|
||
return getClusterDomainName(f) | ||
} | ||
|
||
func getClusterDomainName(r io.Reader) (string, error) { | ||
scanner := bufio.NewScanner(r) | ||
for scanner.Scan() { | ||
elements := strings.Split(scanner.Text(), " ") | ||
if elements[0] != "search" { | ||
continue | ||
} | ||
for i := 1; i < len(elements)-1; i++ { | ||
if strings.HasPrefix(elements[i], "svc.") { | ||
return elements[i][4:], nil | ||
} | ||
} | ||
} | ||
return "", fmt.Errorf("%s does not seem to be a valid kubernetes resolv.conf file", resolverFileName) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Copyright 2018 The Knative Authors | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package utils | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetDomainName(t *testing.T) { | ||
tests := []struct { | ||
descr string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/descr/name/? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
resolvConf string | ||
domainName string | ||
shouldFail bool | ||
}{ | ||
{ | ||
descr: "all good", | ||
resolvConf: ` | ||
nameserver 1.1.1.1 | ||
search default.svc.abc.com svc.abc.com abc.com | ||
options ndots:5 | ||
`, | ||
domainName: "abc.com", | ||
shouldFail: false, | ||
}, | ||
{ | ||
descr: "missing search line", | ||
resolvConf: ` | ||
nameserver 1.1.1.1 | ||
options ndots:5 | ||
`, | ||
domainName: "", | ||
shouldFail: true, | ||
}, | ||
{ | ||
descr: "non k8s resolv.conf format", | ||
resolvConf: ` | ||
nameserver 1.1.1.1 | ||
search abc.com xyz.com | ||
options ndots:5 | ||
`, | ||
domainName: "", | ||
shouldFail: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
dn, err := getClusterDomainName(strings.NewReader(tt.resolvConf)) | ||
if err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this sequence, it does not catch directly func returns err == nil but the expectation was to fail. Do you think it is sufficient to catch this condition just on domain name mismatch? Currently err==nil validation covers 2 scenarios, 1 incorrect parsing of domain name when func returns nil and wrong domain name and 2 not detecting corruption of resolv.conf file. Please let me know if this explanation makes sense to you. Appreciate a lot your feedback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if !tt.shouldFail { | ||
t.Errorf("Test %s failed with error: %q but is not supposed to.", tt.descr, err) | ||
} else { | ||
continue | ||
} | ||
} | ||
if err == nil { | ||
if tt.shouldFail { | ||
t.Errorf("Test %s succeeded but supposed to fail.", tt.descr) | ||
continue | ||
} | ||
if dn != tt.domainName { | ||
t.Errorf("Test %s failed expected %s but got %s", tt.descr, tt.domainName, dn) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we compute this once when the binary starts up so that we won't read a file everytime we want to get the full name of a K8s Service?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this operation is infrequent so the should not be any quantifiable performance impact. Also this approach allows different reaction of a failure of getting domain name. In some cases it might be useful. Appreciate other folks to chime in and share their opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's going to be constant, I don't see reasons for it to be recomputed each time.
If we need to test failure cases, then go provides various methods to spoof failures (substitute files, use different file path, mockout internal function).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, does it work for you, if I call it from main, set global var in utils package and then when domain name is needed utils.ClusterDomainName var can be used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather, I'd recommend to use this: https://golang.org/pkg/sync/#Once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see new solution based on once.Do()