aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/application/v4/model/DeploymentData.java
blob: fd4a34118c536c77d32964a97bea37219244bbb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.application.v4.model;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.DockerImage;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Quota;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.DataplaneTokenVersions;
import com.yahoo.vespa.hosted.controller.api.integration.secrets.TenantSecretStore;
import com.yahoo.yolean.concurrent.Memoized;

import java.io.InputStream;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.Objects.requireNonNull;

/**
 * Data pertaining to a deployment to be done on a config server.
 * Accessor names must match the names in com.yahoo.vespa.config.server.session.PrepareParams.
 *
 * @author jonmv
 */
public class DeploymentData {

    private static final Logger log = Logger.getLogger(DeploymentData.class.getName());

    private final ApplicationId instance;
    private final ZoneId zone;
    private final Supplier<InputStream> applicationPackage;
    private final Version platform;
    private final Supplier<DeploymentEndpoints> endpoints;
    private final Optional<DockerImage> dockerImageRepo;
    private final Optional<AthenzDomain> athenzDomain;
    private final Supplier<Quota> quota;
    private final List<TenantSecretStore> tenantSecretStores;
    private final List<X509Certificate> operatorCertificates;
    private final Supplier<Optional<CloudAccount>> cloudAccount;
    private final Supplier<List<DataplaneTokenVersions>> dataPlaneTokens;
    private final boolean dryRun;

    public DeploymentData(ApplicationId instance, ZoneId zone, Supplier<InputStream> applicationPackage, Version platform,
                          Supplier<DeploymentEndpoints> endpoints,
                          Optional<DockerImage> dockerImageRepo,
                          Optional<AthenzDomain> athenzDomain,
                          Supplier<Quota> quota,
                          List<TenantSecretStore> tenantSecretStores,
                          List<X509Certificate> operatorCertificates,
                          Supplier<Optional<CloudAccount>> cloudAccount,
                          Supplier<List<DataplaneTokenVersions>> dataPlaneTokens,
                          boolean dryRun) {
        this.instance = requireNonNull(instance);
        this.zone = requireNonNull(zone);
        this.applicationPackage = requireNonNull(applicationPackage);
        this.platform = requireNonNull(platform);
        this.endpoints = wrap(requireNonNull(endpoints), Duration.ofSeconds(30), "deployment endpoints for " + instance + " in " + zone);
        this.dockerImageRepo = requireNonNull(dockerImageRepo);
        this.athenzDomain = athenzDomain;
        this.quota = wrap(requireNonNull(quota), Duration.ofSeconds(10), "quota for " + instance);
        this.tenantSecretStores = List.copyOf(requireNonNull(tenantSecretStores));
        this.operatorCertificates = List.copyOf(requireNonNull(operatorCertificates));
        this.cloudAccount = wrap(requireNonNull(cloudAccount), Duration.ofSeconds(5), "cloud account for " + instance + " in " + zone);
        this.dataPlaneTokens = wrap(dataPlaneTokens, Duration.ofSeconds(5), "data plane tokens for " + instance + " in " + zone);
        this.dryRun = dryRun;
    }

    public ApplicationId instance() {
        return instance;
    }

    public ZoneId zone() {
        return zone;
    }

    public InputStream applicationPackage() {
        return applicationPackage.get();
    }

    public Version platform() {
        return platform;
    }

    public DeploymentEndpoints endpoints() {
        return endpoints.get();
    }

    public Optional<DockerImage> dockerImageRepo() {
        return dockerImageRepo;
    }

    public Optional<AthenzDomain> athenzDomain() {
        return athenzDomain;
    }

    public Quota quota() {
        return quota.get();
    }

    public List<TenantSecretStore> tenantSecretStores() {
        return tenantSecretStores;
    }

    public List<X509Certificate> operatorCertificates() {
        return operatorCertificates;
    }

    public Optional<CloudAccount> cloudAccount() {
        return cloudAccount.get();
    }

    public List<DataplaneTokenVersions> dataPlaneTokens() {
        return dataPlaneTokens.get();
    }

    public boolean isDryRun() {
        return dryRun;
    }

    private static <T> Supplier<T> wrap(Supplier<T> delegate, Duration timeout, String description) {
        return new TimingSupplier<>(new Memoized<>(delegate), timeout, description);
    }

    public static class TimingSupplier<T> implements Supplier<T> {

        private final Supplier<T> delegate;
        private final Duration timeout;
        private final String description;

        public TimingSupplier(Supplier<T> delegate, Duration timeout, String description) {
            this.delegate = delegate;
            this.timeout = timeout;
            this.description = description;
        }

        @Override
        public T get() {
            long startNanos = System.nanoTime();
            Throwable thrown = null;
            try {
                return delegate.get();
            }
            catch (Throwable t) {
                thrown = t;
                throw t;
            }
            finally {
                long durationNanos = System.nanoTime() - startNanos;
                Level level = durationNanos > timeout.toNanos() ? Level.WARNING : Level.FINE;
                String thrownMessage = thrown == null ? "" : " with exception " + thrown;
                log.log(level, () -> String.format("Getting %s took %.6f seconds%s", description, durationNanos / 1e9, thrownMessage));
            }
        }

    }

}