worker: split NewClient()
Use the default dialing functions for tcp connections and set the tls config on the transport directly. This makes the code easier to follow, because the only special case is overriding the DialContext() for unix connections.
This commit is contained in:
parent
845ba6e8e5
commit
94183d14a8
1 changed files with 27 additions and 23 deletions
|
|
@ -23,6 +23,7 @@ const RemoteWorkerPort = 8700
|
|||
|
||||
type ComposerClient struct {
|
||||
client *http.Client
|
||||
scheme string
|
||||
hostname string
|
||||
}
|
||||
|
||||
|
|
@ -55,32 +56,33 @@ func createTLSConfig(config *connectionConfig) (*tls.Config, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func newConnection(remoteAddress string, conf *tls.Config) (net.Conn, error) {
|
||||
if remoteAddress != "" {
|
||||
return tls.Dial("tcp", remoteAddress, conf)
|
||||
}
|
||||
|
||||
// plain non-encrypted connection
|
||||
return net.Dial("unix", "/run/osbuild-composer/job.socket")
|
||||
|
||||
}
|
||||
|
||||
func NewClient(remoteAddress string, conf *tls.Config) *ComposerClient {
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: conf,
|
||||
},
|
||||
}
|
||||
|
||||
var scheme string
|
||||
if conf != nil {
|
||||
scheme = "http"
|
||||
} else {
|
||||
scheme = "https"
|
||||
}
|
||||
|
||||
return &ComposerClient{client, scheme, remoteAddress}
|
||||
}
|
||||
|
||||
func NewClientUnix(path string) *ComposerClient {
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DialContext: func(context context.Context, network, addr string) (net.Conn, error) {
|
||||
return newConnection(remoteAddress, conf)
|
||||
return net.Dial("unix", path)
|
||||
},
|
||||
},
|
||||
}
|
||||
hostname := remoteAddress
|
||||
|
||||
// in case of unix domain socket, use localhost as hostname
|
||||
if hostname == "" {
|
||||
hostname = "localhost"
|
||||
}
|
||||
|
||||
return &ComposerClient{client, hostname}
|
||||
return &ComposerClient{client, "http", "localhost"}
|
||||
}
|
||||
|
||||
func (c *ComposerClient) AddJob() (*jobqueue.Job, error) {
|
||||
|
|
@ -149,7 +151,7 @@ func (c *ComposerClient) UploadImage(job *jobqueue.Job, reader io.Reader) error
|
|||
}
|
||||
|
||||
func (c *ComposerClient) createURL(path string) string {
|
||||
return "http://" + c.hostname + path
|
||||
return c.scheme + "://" + c.hostname + path
|
||||
}
|
||||
|
||||
func handleJob(client *ComposerClient) error {
|
||||
|
|
@ -179,12 +181,11 @@ func main() {
|
|||
flag.StringVar(&remoteAddress, "remote", "", "Connect to a remote composer using the specified address")
|
||||
flag.Parse()
|
||||
|
||||
var conf *tls.Config
|
||||
var client *ComposerClient
|
||||
if remoteAddress != "" {
|
||||
remoteAddress = fmt.Sprintf("%s:%d", remoteAddress, RemoteWorkerPort)
|
||||
|
||||
var err error
|
||||
conf, err = createTLSConfig(&connectionConfig{
|
||||
conf, err := createTLSConfig(&connectionConfig{
|
||||
CACertFile: "/etc/osbuild-composer/ca-crt.pem",
|
||||
ClientKeyFile: "/etc/osbuild-composer/worker-key.pem",
|
||||
ClientCertFile: "/etc/osbuild-composer/worker-crt.pem",
|
||||
|
|
@ -192,9 +193,12 @@ func main() {
|
|||
if err != nil {
|
||||
log.Fatalf("Error creating TLS config: %v", err)
|
||||
}
|
||||
|
||||
client = NewClient(remoteAddress, conf)
|
||||
} else {
|
||||
client = NewClientUnix("/run/osbuild-composer/job.socket")
|
||||
}
|
||||
|
||||
client := NewClient(remoteAddress, conf)
|
||||
for {
|
||||
if err := handleJob(client); err != nil {
|
||||
log.Fatalf("Failed to handle job: " + err.Error())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue